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 
00070         mSourceLocation = inSourceLocation;
00071         //debugLog2<<"Setup Socket:"<<endl;
00072         IN_ADDR locAddress;  //iaHost
00073         LPHOSTENT locHostData;;  //lpHost
00074 
00075         bool locValidURL = splitURL(inSourceLocation);
00076 
00077         locAddress.S_un.S_addr = inet_addr(mServerName.c_str());
00078         
00079 
00080         if (locAddress.S_un.S_addr == INADDR_NONE) {
00081                 locHostData = gethostbyname(mServerName.c_str());
00082         } else {
00083                 locHostData = gethostbyaddr((const char*)&locAddress, sizeof(struct in_addr), AF_INET);
00084         }
00085 
00086 
00087 
00088         if (locHostData == NULL) {
00089                 //debugLog2<<"LocHostData is NULL"<<endl;
00090                 //Failed
00091                 return false;
00092         }
00093 
00094         mSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00095         if (mSocket == INVALID_SOCKET) {
00096                 //debugLog2<<"Socket Invalid"<<endl;
00097                 //Failed
00098                 return false;
00099         }
00100 
00101 
00102         LPSERVENT locServiceData; //lpServEnt
00103         SOCKADDR_IN locServiceSocketAddr; //saServer
00104         
00105         if (mPort == 0) {
00106                 locServiceData = getservbyname("http", "tcp");
00107                 if (locServiceData == NULL) {
00108                         locServiceSocketAddr.sin_port = htons(80);
00109                 } else {
00110                         locServiceSocketAddr.sin_port = locServiceData->s_port;
00111                 }
00112         } else {
00113                 //Explicit port
00114                 locServiceSocketAddr.sin_port = htons(mPort);
00115         }
00116 
00117 
00118 
00119         locServiceSocketAddr.sin_family = AF_INET;
00120         locServiceSocketAddr.sin_addr = *((LPIN_ADDR)*locHostData->h_addr_list);
00121 
00122 
00123         int locRetVal = 0;
00124         locRetVal = connect(mSocket, (LPSOCKADDR)&locServiceSocketAddr, sizeof(SOCKADDR_IN));
00125         if (locRetVal == SOCKET_ERROR) {
00126                 //debugLog2<<"Failed to connect..."<<endl;
00127                 closesocket(mSocket);
00128                 return false;
00129         }
00130 
00131         return true;
00132 
00133 
00134 }
00135 
00136 string HTTPSocket::assembleRequest(string inFilePath, unsigned long inStartByte) {
00137         string retRequest;
00138         retRequest = "GET " + inFilePath+ " HTTP/1.1\r\n" + "Host: " + mServerName+ "\r\n" + "Connection: close";
00139         
00140         if (inStartByte != 0) {
00141                 retRequest = retRequest + "\r\n" + "Range: bytes=" + StringHelper::numToString(inStartByte) + "-";
00142         }
00143         
00144         retRequest += "\r\n\r\n";
00145         //debugLog2<<"Assembled Req : "<<endl<<retRequest<<endl;
00146         return retRequest;
00147 }
00148 
00149 bool HTTPSocket::httpRequest(string inRequest) {
00150         //debugLog2<<"Http Request:"<<endl;
00151         int locRetVal = send(mSocket, inRequest.c_str(), (int)inRequest.length(), 0);
00152 
00153         if (locRetVal == SOCKET_ERROR) {
00154                 //debugLog2<<"Socket error on send"<<endl;
00155                 closesocket(mSocket);
00156                 return false;
00157         }
00158         return true;
00159 }
00160 
00161 bool HTTPSocket::splitURL(string inURL) {
00162         //debugLog2<<"Split url:"<<endl;
00163         string locProtocol;
00164         string locServerName;
00165         string locPath;
00166         string locPort;
00167         string locTemp;
00168         size_t locPos2;
00169         size_t locPos = inURL.find(':');
00170         if (locPos == string::npos) {
00171                 //No colon... not a url or file... failure.
00172                 return false;
00173         } else {
00174                 locProtocol = inURL.substr(0, locPos);
00175                 locTemp = inURL.substr(locPos+1);
00176                 locPos = locTemp.find("//");
00177                 if ((locPos == string::npos) || (locPos != 0)) {
00178                         return false;
00179                 } else {
00180             locTemp = locTemp.substr(locPos+2);
00181                         locPos = locTemp.find('/');
00182                         if (locPos == string::npos) {
00183                                 return false;
00184                         } else {
00185                                 locPos2 = locTemp.find(':');
00186                                 if (locPos2 == string::npos) {
00187                                         locServerName = locTemp.substr(0, locPos);
00188                                         locPath = locTemp.substr(locPos);
00189                                 } else if (locPos2 < locPos) {
00190                                         //Explicit port specification
00191                                         locPort = locTemp.substr(locPos2 + 1, locPos - locPos2 - 1);
00192                                         locServerName = locTemp.substr(0, locPos2);
00193                                         locPath = locTemp.substr(locPos);
00194                                 }
00195 
00196                         }
00197                 }
00198                 
00199         }
00200 
00201         mServerName = locServerName;
00202         mFileName = locPath;
00203         if (locPort != "") {
00204                 //TODO::: Error checking needed
00205                 mPort = atoi(locPort.c_str());
00206         } else {
00207                 mPort = 0;
00208         }
00209         //debugLog2<<"Proto : "<<locProtocol<<endl<<"Server : "<<locServerName<<endl<<" Path : "<<mFileName<<" Port : "<<mPort<<endl;
00210         return true;
00211 
00212 }
00213 void HTTPSocket::closeSocket() {
00214         //debugLog2<<"Close Socket:"<<endl;
00215         int ret = closesocket(mSocket);
00216         ret = ret;
00217 }

Generated on Thu Feb 16 23:48:12 2006 for oggdsf by  doxygen 1.3.9