GeoIpRequest.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file GeoIpRequest.cpp
00013 ** \version $Id: GeoIpRequest.cpp 3768 2009-05-13 19:07:26Z edmanm $
00014 ** \brief A formatted request for GeoIP information for one or more IPs
00015 */
00016 
00017 #include "GeoIpRequest.h"
00018 #include "ZlibByteArray.h"
00019 
00020 #include <QString>
00021 #include <QHostAddress>
00022 #include <QHttpRequestHeader>
00023 
00024 
00025 /** Creates an HTTP POST header for this request, based on the 
00026  * Host, Page, and content-length values. */
00027 QHttpRequestHeader
00028 GeoIpRequest::createHeader() const
00029 {
00030   QHttpRequestHeader header("POST", _page, 1, 1);
00031   
00032   if (!_host.isEmpty())
00033     header.setValue("Host", _host);
00034   header.setContentType("application/x-www-form-urlencoded");
00035   header.setContentLength(_request.length());
00036   header.setValue("Connection", "close");
00037 
00038   if (ZlibByteArray::isZlibAvailable()) {
00039     QString acceptEncodings = "deflate, x-deflate";
00040     if (ZlibByteArray::isGzipSupported())
00041       acceptEncodings += ", gzip, x-gzip";
00042     header.setValue("Accept-Encoding", acceptEncodings);
00043   }
00044 
00045   return header;
00046 }
00047 
00048 /** Sets the list of IPs whose geo information we want to request. */
00049 void
00050 GeoIpRequest::setRequest(const QList<QHostAddress> &ips)
00051 {
00052   _request = "format=long&ip=";
00053   int ipcount = ips.size();
00054 
00055   /* Add each IP to a comma-delimited list. */
00056   for (int i = 0; i < ipcount; i++) {
00057     _request.append(ips.at(i).toString());
00058     if (i < ipcount-1) {
00059       _request.append(",");
00060     }
00061   }
00062   _ips = ips;
00063 }
00064 
00065 /** Formats the request as an HTTP POST request. */
00066 QByteArray
00067 GeoIpRequest::request() const
00068 {
00069   /* Create the header and append the request content. */
00070   QString request = createHeader().toString() + _request;
00071   return request.toAscii();
00072 }
00073 
00074 /** Returns true if this request contains <b>ip</b>. */
00075 bool
00076 GeoIpRequest::contains(const QHostAddress &ip) const
00077 {
00078   return _ips.contains(ip);
00079 }
00080 
Generated on Mon Aug 30 22:58:54 2010 for Vidalia by  doxygen 1.6.3