00001
00002
00003
00004
00005
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
00020 HttpSocket::HttpSocket(char* host, int port) : Socket(host, Socket::tcp, port, 1024) {
00021 this->host = host;
00022 }
00023
00024
00025 HttpSocket::~HttpSocket() {
00026
00027 }
00028
00029
00030
00031
00032
00033
00034
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
00047 int bytesSent = Send(headerData, headerSize);
00048 if (bytesSent != headerSize) {
00049
00050 return NULL;
00051 }
00052 else {
00053
00054 bytesSent = Send(requestData, requestSize);
00055 if (bytesSent != requestSize) {
00056
00057 return NULL;
00058 }
00059 else {
00060
00061 char line[256];
00062 int lineLength;
00063
00064
00065 do {
00066 lineLength = 0;
00067 char character[1];
00068 do {
00069 if (Receive(character, 1) == 0) {
00070
00071 return NULL;
00072 }
00073 line[lineLength] = character[0];
00074 lineLength++;
00075 }
00076 while (character[0] != '\n');
00077 line[lineLength] = '\0';
00078
00079
00080 if ((strstr(line, "Content-length:")) || (strstr(line, "Content-Length:"))) {
00081
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
00094 char* responseData = new char[responseSize];
00095 Receive(responseData, responseSize);
00096
00097
00098 return responseData;
00099 }
00100 }
00101 }