diff --git network/network_file_transfer.cc network/network_file_transfer.cc index 5a1065bbe..77393f28b 100644 --- network/network_file_transfer.cc +++ network/network_file_transfer.cc @@ -6,6 +6,7 @@ #include #include #include "../utils/cbuffer_t.h" +#include "../utils/simstring.h" #ifndef NETTOOL #include "../dataobj/translator.h" @@ -79,6 +80,83 @@ char const* network_receive_file( SOCKET const s, char const* const save_as, sin return NULL; } +const char *network_http_get ( const char* address, const char* name, cbuffer_t& local ) +{ + const int REQ_HEADER_LEN = 1024; + // open from network + const char *err = NULL; + SOCKET const my_client_socket = network_open_address( address, err ); + if ( err==NULL ) { +#ifndef REVISION +# define REVISION 0 +#endif + const char* format = "GET %s HTTP/1.1\r\n" + "User-Agent: Simutrans/r%s\r\n" + "Host: %s\r\n\r\n"; + if ( (strlen( format ) + strlen( name ) + strlen( address ) + strlen( QUOTEME(REVISION)) ) > ( REQ_HEADER_LEN - 1 ) ) { + // We will get a buffer overwrite here if we continue + return "Error: String too long"; + } + char request[REQ_HEADER_LEN]; + int const len = sprintf( request, format, name, QUOTEME(REVISION), address ); + uint16 dummy; + if ( !network_send_data( my_client_socket, request, len, dummy, 250 ) ) { + err = "Server did not respond!"; + } + + // Read the response header and parse arguments as needed + char line[1024], rbuf; + unsigned int pos = 0; + sint32 length = 0; + while(1) { + // Receive one character at a time the HTTP headers + int i = recv( my_client_socket, &rbuf, 1, 0 ); + if ( i > 0 ) { + if ( rbuf >= 32 && pos < sizeof(line) - 1 ) { + line[pos++] = rbuf; + } + if ( rbuf == 10 ) { + if ( pos == 0 ) { + // this line was empty => now data will follow + break; + } + line[pos] = 0; + DBG_MESSAGE( "network_http_get", "received header: %s", line ); + // Parse out the length tag to get length of content + if ( STRNICMP( "Content-Length:", line, 15 ) == 0 ) { + length = atol( line + 15 ); + } + pos = 0; + } + } + else { + break; + } + } + + // Make buffer to receive data into + char* buffer = new char[length]; + uint16 bytesreceived = 0; + + if ( !network_receive_data( my_client_socket, buffer, length, bytesreceived, 10000 ) ) { + err = "Error: network_receive_data failed!"; + } + else if ( bytesreceived != length ) { + err = "Error: Bytes received does not match length!"; + } + else { + local.append( buffer, length ); + } + + DBG_MESSAGE( "network_http_get", "received data length: %i", local.len() ); + + delete [] buffer; + network_close_socket( my_client_socket ); + } + return err; +} + + /* * Functions not needed by Nettool following below */ @@ -393,80 +471,5 @@ const char *network_http_post( const char *address, const char *name, const char return err; } -const char *network_http_get ( const char* address, const char* name, cbuffer_t& local ) -{ - const int REQ_HEADER_LEN = 1024; - // open from network - const char *err = NULL; - SOCKET const my_client_socket = network_open_address( address, err ); - if ( err==NULL ) { -#ifndef REVISION -# define REVISION 0 -#endif - const char* format = "GET %s HTTP/1.1\r\n" - "User-Agent: Simutrans/r%s\r\n" - "Host: %s\r\n\r\n"; - if ( (strlen( format ) + strlen( name ) + strlen( address ) + strlen( QUOTEME(REVISION)) ) > ( REQ_HEADER_LEN - 1 ) ) { - // We will get a buffer overwrite here if we continue - return "Error: String too long"; - } - char request[REQ_HEADER_LEN]; - int const len = sprintf( request, format, name, QUOTEME(REVISION), address ); - uint16 dummy; - if ( !network_send_data( my_client_socket, request, len, dummy, 250 ) ) { - err = "Server did not respond!"; - } - - // Read the response header and parse arguments as needed - char line[1024], rbuf; - unsigned int pos = 0; - sint32 length = 0; - while(1) { - // Receive one character at a time the HTTP headers - int i = recv( my_client_socket, &rbuf, 1, 0 ); - if ( i > 0 ) { - if ( rbuf >= 32 && pos < sizeof(line) - 1 ) { - line[pos++] = rbuf; - } - if ( rbuf == 10 ) { - if ( pos == 0 ) { - // this line was empty => now data will follow - break; - } - line[pos] = 0; - DBG_MESSAGE( "network_http_get", "received header: %s", line ); - // Parse out the length tag to get length of content - if ( STRNICMP( "Content-Length:", line, 15 ) == 0 ) { - length = atol( line + 15 ); - } - pos = 0; - } - } - else { - break; - } - } - - // Make buffer to receive data into - char* buffer = new char[length]; - uint16 bytesreceived = 0; - - if ( !network_receive_data( my_client_socket, buffer, length, bytesreceived, 10000 ) ) { - err = "Error: network_receive_data failed!"; - } - else if ( bytesreceived != length ) { - err = "Error: Bytes received does not match length!"; - } - else { - local.append( buffer, length ); - } - - DBG_MESSAGE( "network_http_get", "received data length: %i", local.len() ); - - delete [] buffer; - network_close_socket( my_client_socket ); - } - return err; -} #endif