geoipresponse.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  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 geoipresponse.cpp
00024  * \version $Id: geoipresponse.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Parses a response to a previous GeoIP request
00026  */
00027 
00028 #include <QStringList>
00029 #include "geoipresponse.h"
00030 
00031 #define HTTP_OK   200     /**< HTTP OK status code. */
00032 
00033 
00034 /** Constructor. Parses the response data for an HTTP header and Geo IP
00035  * information. */
00036 GeoIpResponse::GeoIpResponse(QByteArray response)
00037 {
00038   QString data(response);
00039 
00040   /* Parse out the header */
00041   int headerPos = data.indexOf("\r\n");
00042   _header = QHttpResponseHeader(data.mid(0, headerPos));
00043  
00044   /* Parse out the Geo IP information, if any was included. */
00045   if (headerPos > 0 && _header.statusCode() == HTTP_OK) {
00046     /* Split the content from the header */
00047     QString content = data.mid(headerPos+2);
00048     
00049     /* Split each response line at the \n */
00050     QStringList lines = content.split("\n");
00051     foreach (QString line, lines) {
00052       /* Parse the Geo IP information */
00053       GeoIp geoip = GeoIp::fromString(line);
00054       if (!geoip.isEmpty()) {
00055         /* Add it to the list of response information */
00056         _geoips << geoip;
00057       }
00058     }
00059   }
00060 }
00061 

Generated on Mon Oct 23 20:08:15 2006 for Vidalia by  doxygen 1.5.0