/Users/craigcornelius/Projects/SPRING Mac Release 0.2/http_socket.cpp

Go to the documentation of this file.
00001 // $Id: http_socket.cpp,v 1.2 2006/03/16 23:32:24 hawaii Exp $
00002 // $Copyright: (c)2001 National Biocomputation Center, Stanford University $
00003 
00004 //
00005 // SEE http_socket.h for further info!
00006 //
00007 
00008 #include "http_socket.h"
00009 
00010 #include <string.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 
00014 
00015 const char* HttpSocket::rcsid = "@(#) $Id: http_socket.cpp,v 1.2 2006/03/16 23:32:24 hawaii Exp $ $Copyright: (c)2001 National Biocomputation Center, Stanford University $";
00016 int HttpSocket::debug = 0;
00017 
00018 
00019 // http socket constructor
00020 HttpSocket::HttpSocket(char* host, int port) : Socket(host, Socket::tcp, port, 1024) {
00021   this->host = host;
00022 }
00023 
00024 // destructor
00025 HttpSocket::~HttpSocket() {
00026 
00027 }
00028 
00029 // send a request using HTTP POST (returns NULL, if an error occured)
00030 // -filePath: e.g. "/", "/projects/index.html", "/projects/index.jsp" ...
00031 // -requestData: any data
00032 // -requestSize: the size of the request data
00033 // -responseSize: the actual size of the data obtained (by reference)
00034 // -RETURN VALUE: The returned data (no header)
00035 char* HttpSocket::postRequest(char* filePath, char* requestData, int requestSize, int &responseSize) {
00036   responseSize = 0;
00037   char headerData[1024];
00038   sprintf(headerData, "POST %s HTTP/1.1\r\n"
00039                           "Host: %s\r\n"
00040                                           "User-Agent: SPRING HttpSocket\r\n"
00041                                           "Content-Length: %i\r\n"
00042                                           "Content-Type: application/octet-stream\r\n"
00043                                           "\r\n\0", filePath, host, requestSize);
00044   int headerSize = strlen(headerData);
00045 
00046   // and send header
00047   int bytesSent = Send(headerData, headerSize);
00048   if (bytesSent != headerSize) {
00049           // error sending header
00050           return NULL;
00051   }
00052   else {
00053       // send data
00054           bytesSent = Send(requestData, requestSize);
00055           if (bytesSent != requestSize) {
00056                 // error occured, did not send all bytes!
00057                 return NULL;
00058           }
00059           else {
00060                 // get data
00061                 char line[256];
00062                 int lineLength;
00063 
00064                 // read header + find content-length!
00065                 do {
00066                   lineLength = 0;
00067                   char character[1];
00068                   do {
00069                         if (Receive(character, 1) == 0) {
00070                                 // error occured
00071                                 return NULL;
00072                         }
00073                         line[lineLength] = character[0];
00074                         lineLength++;
00075                   } 
00076                   while (character[0] != '\n');
00077                   line[lineLength] = '\0';
00078 
00079                   // check if length description
00080                   if ((strstr(line, "Content-length:")) || (strstr(line, "Content-Length:"))) {
00081                         // find length
00082                         int index = lineLength - 3;
00083                         int multiplier = 1;
00084                         while ((line[index] != ' ') && (line[index] != ':')) {
00085                           responseSize += (line[index] - '0') * multiplier;
00086                           multiplier *= 10;
00087                           index--;
00088                         }
00089                   }
00090                 }
00091                 while (line[0] != '\r');
00092 
00093                 // read data
00094                 char* responseData = new char[responseSize];
00095                 Receive(responseData, responseSize);
00096 
00097                 // and return data
00098                 return responseData;
00099           }
00100   }
00101 }

Generated on Thu Aug 30 11:03:14 2007 for SPRING Mac by  doxygen 1.5.3