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 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief A formatted request for GeoIP information for one or more IPs
00015 */
00016 
00017 #include <zlibbytearray.h>
00018 
00019 #include "geoiprequest.h"
00020 
00021 
00022 /** Creates an HTTP POST header for this request, based on the 
00023  * Host, Page, and content-length values. */
00024 QHttpRequestHeader
00025 GeoIpRequest::createHeader() const
00026 {
00027   QHttpRequestHeader header("POST", _page, 1, 1);
00028   
00029   if (!_host.isEmpty())
00030     header.setValue("Host", _host);
00031   header.setContentType("application/x-www-form-urlencoded");
00032   header.setContentLength(_request.length());
00033   header.setValue("Connection", "close");
00034 
00035   if (ZlibByteArray::isZlibAvailable()) {
00036     QString acceptEncodings = "deflate, x-deflate";
00037     if (ZlibByteArray::isGzipSupported())
00038       acceptEncodings += ", gzip, x-gzip";
00039     header.setValue("Accept-Encoding", acceptEncodings);
00040   }
00041 
00042   return header;
00043 }
00044 
00045 /** Sets the list of IPs whose geo information we want to request. */
00046 void
00047 GeoIpRequest::setRequest(const QList<QHostAddress> &ips)
00048 {
00049   _request = "ip=";
00050   int ipcount = ips.size();
00051 
00052   /* Add each IP to a comma-delimited list. */
00053   for (int i = 0; i < ipcount; i++) {
00054     _request.append(ips.at(i).toString());
00055     if (i < ipcount-1) {
00056       _request.append(",");
00057     }
00058   }
00059   _ips = ips;
00060 }
00061 
00062 /** Formats the request as an HTTP POST request. */
00063 QByteArray
00064 GeoIpRequest::request() const
00065 {
00066   /* Create the header and append the request content. */
00067   QString request = createHeader().toString() + _request;
00068   return request.toAscii();
00069 }
00070 
00071 /** Returns true if this request contains <b>ip</b>. */
00072 bool
00073 GeoIpRequest::contains(const QHostAddress &ip) const
00074 {
00075   return _ips.contains(ip);
00076 }
00077 

Generated on Tue Jul 7 16:58:11 2009 for Vidalia by  doxygen 1.4.7