HTTPSocket.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh
00003 //
00004 //Redistribution and use in source and binary forms, with or without
00005 //modification, are permitted provided that the following conditions
00006 //are met:
00007 //
00008 //- Redistributions of source code must retain the above copyright
00009 //  notice, this list of conditions and the following disclaimer.
00010 //
00011 //- Redistributions in binary form must reproduce the above copyright
00012 //  notice, this list of conditions and the following disclaimer in the
00013 //  documentation and/or other materials provided with the distribution.
00014 //
00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00016 //  may be used to endorse or promote products derived from this software 
00017 //  without specific prior written permission.
00018 //
00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //===========================================================================
00031 #include "stdafx.h"
00032 #include ".\httpsocket.h"
00033 
00034 HTTPSocket::HTTPSocket(void)
00035         :       mWasError(false)
00036         ,       mIsEOF(false)
00037         ,       mIsOpen(false)
00038         ,       mSeenResponse(false)
00039 {
00040         //debugLog2.open("G:\\logs\\httpsocket.log", ios_base::out);
00041 
00042         //Setup the socket API
00043         WORD locWinsockVersion = MAKEWORD(1,1);
00044         WSADATA locWinsockData;
00045         int locRet= 0;
00046 
00047         locRet = WSAStartup(locWinsockVersion, &locWinsockData);
00048         if ((locRet != 0) || (locWinsockData.wVersion != locWinsockVersion)) {
00049                 //Failed to setup.
00050                 //debugLog2<<"Failed to start winsock V "<<locWinsockData.wVersion<<endl;
00051                 WSACleanup();
00052                 throw 0;
00053         }
00054 
00055         //debugLog2<<"Winsock started"<<endl;
00056 }
00057 
00058 HTTPSocket::~HTTPSocket(void)
00059 {
00060         //debugLog2<<"Winsock ended"<<endl;
00061         //debugLog2.close();
00062         
00063         WSACleanup();
00064 }
00065 
00066 
00067 bool HTTPSocket::setupSocket(string inSourceLocation) {
00068         
00069         //debugLog2<<"Setup Socket:"<<endl;
00070         IN_ADDR locAddress;  //iaHost
00071         LPHOSTENT locHostData;;  //lpHost
00072 
00073         bool locValidURL = splitURL(inSourceLocation);
00074 
00075         locAddress.S_un.S_addr = inet_addr(mServerName.c_str());
00076         
00077 
00078         if (locAddress.S_un.S_addr == INADDR_NONE) {
00079                 locHostData = gethostbyname(mServerName.c_str());
00080         } else {
00081                 locHostData = gethostbyaddr((const char*)&locAddress, sizeof(struct in_addr), AF_INET);
00082         }
00083 
00084 
00085 
00086         if (locHostData == NULL) {
00087                 //debugLog2<<"LocHostData is NULL"<<endl;
00088                 //Failed
00089                 return false;
00090         }
00091 
00092         mSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00093         if (mSocket == INVALID_SOCKET) {
00094                 //debugLog2<<"Socket Invalid"<<endl;
00095                 //Failed
00096                 return false;
00097         }
00098 
00099 
00100         LPSERVENT locServiceData; //lpServEnt
00101         SOCKADDR_IN locServiceSocketAddr; //saServer
00102         
00103         if (mPort == 0) {
00104                 locServiceData = getservbyname("http", "tcp");
00105                 if (locServiceData == NULL) {
00106                         locServiceSocketAddr.sin_port = htons(80);
00107                 } else {
00108                         locServiceSocketAddr.sin_port = locServiceData->s_port;
00109                 }
00110         } else {
00111                 //Explicit port
00112                 locServiceSocketAddr.sin_port = htons(mPort);
00113         }
00114 
00115 
00116 
00117         locServiceSocketAddr.sin_family = AF_INET;
00118         locServiceSocketAddr.sin_addr = *((LPIN_ADDR)*locHostData->h_addr_list);
00119 
00120 
00121         int locRetVal = 0;
00122         locRetVal = connect(mSocket, (LPSOCKADDR)&locServiceSocketAddr, sizeof(SOCKADDR_IN));
00123         if (locRetVal == SOCKET_ERROR) {
00124                 //debugLog2<<"Failed to connect..."<<endl;
00125                 closesocket(mSocket);
00126                 return false;
00127         }
00128 
00129         return true;
00130 
00131 
00132 }
00133 
00134 string HTTPSocket::assembleRequest(string inFilePath) {
00135         string retRequest;
00136         retRequest = "GET " + inFilePath+ " HTTP/1.1\r\n" + "Host: " + mServerName+ "\r\n" + "Connection: close" + "\r\n\r\n";
00137         //debugLog2<<"Assembled Req : "<<endl<<retRequest<<endl;
00138         return retRequest;
00139 }
00140 
00141 bool HTTPSocket::httpRequest(string inRequest) {
00142         //debugLog2<<"Http Request:"<<endl;
00143         int locRetVal = send(mSocket, inRequest.c_str(), (int)inRequest.length(), 0);
00144 
00145         if (locRetVal == SOCKET_ERROR) {
00146                 //debugLog2<<"Socket error on send"<<endl;
00147                 closesocket(mSocket);
00148                 return false;
00149         }
00150         return true;
00151 }
00152 
00153 bool HTTPSocket::splitURL(string inURL) {
00154         //debugLog2<<"Split url:"<<endl;
00155         string locProtocol;
00156         string locServerName;
00157         string locPath;
00158         string locPort;
00159         string locTemp;
00160         size_t locPos2;
00161         size_t locPos = inURL.find(':');
00162         if (locPos == string::npos) {
00163                 //No colon... not a url or file... failure.
00164                 return false;
00165         } else {
00166                 locProtocol = inURL.substr(0, locPos);
00167                 locTemp = inURL.substr(locPos+1);
00168                 locPos = locTemp.find("//");
00169                 if ((locPos == string::npos) || (locPos != 0)) {
00170                         return false;
00171                 } else {
00172             locTemp = locTemp.substr(locPos+2);
00173                         locPos = locTemp.find('/');
00174                         if (locPos == string::npos) {
00175                                 return false;
00176                         } else {
00177                                 locPos2 = locTemp.find(':');
00178                                 if (locPos2 == string::npos) {
00179                                         locServerName = locTemp.substr(0, locPos);
00180                                         locPath = locTemp.substr(locPos);
00181                                 } else if (locPos2 < locPos) {
00182                                         //Explicit port specification
00183                                         locPort = locTemp.substr(locPos2 + 1, locPos - locPos2 - 1);
00184                                         locServerName = locTemp.substr(0, locPos2);
00185                                         locPath = locTemp.substr(locPos);
00186                                 }
00187 
00188                         }
00189                 }
00190                 
00191         }
00192 
00193         mServerName = locServerName;
00194         mFileName = locPath;
00195         if (locPort != "") {
00196                 //TODO::: Error checking needed
00197                 mPort = atoi(locPort.c_str());
00198         } else {
00199                 mPort = 0;
00200         }
00201         //debugLog2<<"Proto : "<<locProtocol<<endl<<"Server : "<<locServerName<<endl<<" Path : "<<mFileName<<" Port : "<<mPort<<endl;
00202         return true;
00203 
00204 }
00205 void HTTPSocket::closeSocket() {
00206         //debugLog2<<"Close Socket:"<<endl;
00207         closesocket(mSocket);
00208 }

Generated on Tue Feb 15 14:54:19 2005 for oggdsf by  doxygen 1.3.9