geoiprequest.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA  02110-1301, USA.
00020  ***************************************************************/
00021 
00022 /**
00023  * \file geoiprequest.cpp
00024  * \version $Id: geoiprequest.cpp 1591 2007-01-15 23:25:55Z edmanm $
00025  * \brief A formatted request for GeoIP information for one or more IPs
00026  */
00027 
00028 #include <util/zlibbytearray.h>
00029 
00030 #include "geoiprequest.h"
00031 
00032 
00033 /** Creates an HTTP POST header for this request, based on the 
00034  * Host, Page, and content-length values. */
00035 QHttpRequestHeader
00036 GeoIpRequest::createHeader()
00037 {
00038   QHttpRequestHeader header("POST", _page, 1, 1);
00039   
00040   if (!_host.isEmpty())
00041     header.setValue("Host", _host);
00042   header.setContentType("application/x-www-form-urlencoded");
00043   header.setContentLength(_request.length());
00044   header.setValue("Connection", "close");
00045 
00046   if (ZlibByteArray::isZlibAvailable()) {
00047     QString acceptEncodings = "deflate, x-deflate";
00048     if (ZlibByteArray::isGzipSupported())
00049       acceptEncodings += ", gzip, x-gzip";
00050     header.setValue("Accept-Encoding", acceptEncodings);
00051   }
00052 
00053   return header;
00054 }
00055 
00056 /** Sets the list of IPs whose geo information we want to request. */
00057 void
00058 GeoIpRequest::setRequest(QList<QHostAddress> ips)
00059 {
00060   _request = "ip=";
00061   int ipcount = ips.size();
00062 
00063   /* Add each IP to a comma-delimited list. */
00064   for (int i = 0; i < ipcount; i++) {
00065     _request.append(ips.at(i).toString());
00066     if (i < ipcount-1) {
00067       _request.append(",");
00068     }
00069   }
00070   _ips = ips;
00071 }
00072 
00073 /** Formats the request as an HTTP POST request. */
00074 QByteArray
00075 GeoIpRequest::request()
00076 {
00077   /* Create the header and append the request content. */
00078   QString request = createHeader().toString() + _request;
00079   return request.toAscii();
00080 }
00081 
00082 /** Returns true if this request contains <b>ip</b>. */
00083 bool
00084 GeoIpRequest::contains(QHostAddress ip)
00085 {
00086   return _ips.contains(ip);
00087 }
00088 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3