//******************************************************************************************************* // SendHTTP: // Main entry point for this code. // url - The URL to GET/POST to/from. // headerSend - Headers to be sent to the server. // post - Data to be posted to the server, NULL if GET. // postLength - Length of data to post. // req - Contains the message and headerSend sent by the server. // // returns 1 on failure, 0 on success. //******************************************************************************************************* int SendHTTP(char* url,HTTPRequest *req) { #ifdef WIN32 WSADATA WsaData; #endif struct sockaddr_in sin; int sock; char buffer[512]; char protocol[20],host[256],request[1024]; int l,port,chars,err; MemBuffer headersBuffer,messageBuffer; char headerSend[1024]; qboolean done; //S4ND_Log("Parse URL\n"); ParseURL(url,protocol,sizeof(protocol),host,sizeof(host), // Parse the URL request,sizeof(request),&port); if(strcmp(protocol,"HTTP")) return 1; #ifdef WIN32 err = WSAStartup (0x0101, &WsaData); // Init Winsock if(err!=0) return 1; #endif //S4ND_Log("Open Socket\n"); sock = socket (AF_INET, SOCK_STREAM, 0); //if (socket == INVALID_SOCKET) if (sock < 0) return 1; sin.sin_family = AF_INET; //Connect to web sever sin.sin_port = htons( (unsigned short)port ); sin.sin_addr.s_addr = GetHostAddress(host); //S4ND_Log("Connect\n"); if( connect (sock,(struct sockaddr*)&sin, sizeof(/*struct sockaddr_in*/sin) ) ) { return 1; } //S4ND_Log("Start Sending\n"); if( !*request ) strcpy(request,"/"); SendString(sock,"GET "); strcpy(headerSend, "GET "); SendString(sock,request); strcat(headerSend, request); SendString(sock," HTTP/1.0\r\n"); strcat(headerSend, " HTTP/1.0\r\n"); SendString(sock,"Accept: image/gif, image/x-xbitmap," " image/jpeg, image/pjpeg, application/vnd.ms-excel," " application/msword, application/vnd.ms-powerpoint," " */*\r\n"); strcat(headerSend, "Accept: image/gif, image/x-xbitmap," " image/jpeg, image/pjpeg, application/vnd.ms-excel," " application/msword, application/vnd.ms-powerpoint," " */*\r\n"); SendString(sock,"Accept-Language: en-us\r\n"); strcat(headerSend, "Accept-Language: en-us\r\n"); SendString(sock,"Accept-Encoding: gzip, deflate\r\n"); strcat(headerSend, "Accept-Encoding: gzip, deflate\r\n"); SendString(sock,"User-Agent: Mozilla/4.0\r\n"); strcat(headerSend, "User-Agent: Mozilla/4.0\r\n"); SendString(sock,"Host: "); strcat(headerSend, "Host: "); SendString(sock,host); strcat(headerSend, host); SendString(sock,"\r\n"); strcat(headerSend, "\r\n"); SendString(sock,"\r\n"); // Send a blank line to signal end of HTTP headerReceive strcat(headerSend, "\r\n"); req->headerSend = (char*) malloc( sizeof(char*) * strlen(headerSend)); strcpy(req->headerSend, (char*) headerSend ); MemBufferCreate(&headersBuffer ); chars = 0; done = qfalse; //S4ND_Log("Start Recv.\n"); while(!done) { l = recv(sock,buffer,1,0); if(l<0) done=qtrue; switch(*buffer) { case '\r': break; case '\n': if(chars==0) done = qtrue; chars=0; break; default: chars++; break; } MemBufferAddByte(&headersBuffer,*buffer); } req->headerReceive = (char*) headersBuffer.buffer; *(headersBuffer.position) = 0; MemBufferCreate(&messageBuffer); // Now read the HTTP body do { l = recv(sock,buffer,sizeof(buffer)-1,0); if(l<0) break; *(buffer+l)=0; MemBufferAddBuffer(&messageBuffer, (unsigned char*)&buffer, l); } while(l>0); *messageBuffer.position = 0; req->message = (char*) messageBuffer.buffer; req->messageLength = (messageBuffer.position - messageBuffer.buffer); //S4ND_Log("Close Socket\n"); closesocket(sock); // Cleanup return 0; }
//******************************************************************************************************* // SendHTTP: // Main entry point for this code. // url - The URL to GET/POST to/from. // headerSend - Headers to be sent to the server. // post - Data to be posted to the server, NULL if GET. // postLength - Length of data to post. // req - Contains the message and headerSend sent by the server. // // returns 1 on failure, 0 on success. //******************************************************************************************************* int Request::SendHTTP(LPCSTR url,LPCSTR headerReceive,BYTE *post, DWORD postLength,HTTPRequest *req) { WSADATA WsaData; SOCKADDR_IN sin; SOCKET sock; char buffer[512]; char protocol[20],host[256],request[1024]; int l,port,chars,err; MemBuffer headersBuffer,messageBuffer; char headerSend[1024]; BOOL done; ParseURL(url,protocol,sizeof(protocol),host,sizeof(host), // Parse the URL request,sizeof(request),&port); if(strcmp(protocol,"HTTP")) return 1; err = WSAStartup (0x0101, &WsaData); // Init Winsock if(err!=0) return 1; sock = socket (AF_INET, SOCK_STREAM, 0); //if (socket == INVALID_SOCKET) if (sock == INVALID_SOCKET) return 1; sin.sin_family = AF_INET; //Connect to web sever sin.sin_port = htons( (unsigned short)port ); sin.sin_addr.s_addr = GetHostAddress(host); if( connect (sock,(LPSOCKADDR)&sin, sizeof(SOCKADDR_IN) ) ) { return 1; } //printf("\r\n\r\n <<SEND HTTP REQUEST : >> \r\n\r\n"); //Send request if( !*request ) lstrcpyn(request,"/",sizeof(request)); if( post == NULL ) { SendString(sock,"GET "); strcpy(headerSend, "GET "); } else { SendString(sock,"POST "); strcpy(headerSend, "POST "); } SendString(sock,request); strcat(headerSend, request); SendString(sock," HTTP/1.0\r\n"); strcat(headerSend, " HTTP/1.0\r\n"); SendString(sock,"Accept: image/gif, image/x-xbitmap," " image/jpeg, image/pjpeg, application/vnd.ms-excel," " application/msword, application/vnd.ms-powerpoint," " */*\r\n"); strcat(headerSend, "Accept: image/gif, image/x-xbitmap," " image/jpeg, image/pjpeg, application/vnd.ms-excel," " application/msword, application/vnd.ms-powerpoint," " */*\r\n"); SendString(sock,"Accept-Language: en-us\r\n"); strcat(headerSend, "Accept-Language: en-us\r\n"); SendString(sock,"Accept-Encoding: gzip, deflate\r\n"); strcat(headerSend, "Accept-Encoding: gzip, deflate\r\n"); SendString(sock,"User-Agent: Mozilla/4.0\r\n"); strcat(headerSend, "User-Agent: Mozilla/4.0\r\n"); if(postLength) { wsprintf(buffer,"Content-Length: %ld\r\n",postLength); SendString(sock,buffer); strcat(headerSend, buffer); } //SendString(sock,"Cookie: mycookie=blablabla\r\n"); // printf("Cookie: mycookie=blablabla\r\n"); SendString(sock,"Host: "); strcat(headerSend, "Host: "); SendString(sock,host); strcat(headerSend, host); SendString(sock,"\r\n"); strcat(headerSend, "\r\n"); if( (headerReceive!=NULL) && *headerReceive ) { SendString(sock,headerReceive); strcat(headerSend, headerReceive); } SendString(sock,"\r\n"); // Send a blank line to signal end of HTTP headerReceive strcat(headerSend, "\r\n"); if( (post!=NULL) && postLength ) { send(sock,(const char*)post,postLength,0); post[postLength] = '\0'; strcat(headerSend, (const char*)post); } //strcpy(req->headerSend, headerSend); req->headerSend = (char*) malloc( sizeof(char*) * strlen(headerSend)); strcpy(req->headerSend, (char*) headerSend ); // req->headerSend = (char*) headerSend ; // free(req->headerSend); // Read the result //printf("\r\n\r\n <<RECEIVE HTTP REQUEST : >> \r\n\r\n\r\n"); // First read HTTP headerReceive MemBufferCreate(&headersBuffer ); chars = 0; done = FALSE; while(!done) { l = recv(sock,buffer,1,0); if(l<0) done=TRUE; switch(*buffer) { case '\r': break; case '\n': if(chars==0) done = TRUE; chars=0; break; default: chars++; break; } MemBufferAddByte(&headersBuffer,*buffer); } req->headerReceive = (char*) headersBuffer.buffer; *(headersBuffer.position) = 0; MemBufferCreate(&messageBuffer); // Now read the HTTP body do { l = recv(sock,buffer,sizeof(buffer)-1,0); if(l<0) break; *(buffer+l)=0; MemBufferAddBuffer(&messageBuffer, (unsigned char*)&buffer, l); } while(l>0); *messageBuffer.position = 0; req->message = (char*) messageBuffer.buffer; req->messageLength = (messageBuffer.position - messageBuffer.buffer); closesocket(sock); // Cleanup return 0; }