X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fhttp.c;h=cfab11fc0dd7a872bf3169ea75d9c4bc4a92393c;hb=70b012b39b70f36082c025de078dd914a6d6a246;hp=74fafb379713e61b5d4d7a9edf768da73090a896;hpb=db269a1ab34d16c03e8087eaf786e7b3c9a135e4;p=rocksndiamonds.git diff --git a/src/libgame/http.c b/src/libgame/http.c index 74fafb37..cfab11fc 100644 --- a/src/libgame/http.c +++ b/src/libgame/http.c @@ -37,7 +37,9 @@ void ConvertHttpRequestBodyToServerEncoding(struct HttpRequest *request) { char *body_utf8 = getUTF8FromLatin1(request->body); - strcpy(request->body, body_utf8); + strncpy(request->body, body_utf8, MAX_HTTP_BODY_SIZE); + request->body[MAX_HTTP_BODY_SIZE] = '\0'; + checked_free(body_utf8); } @@ -45,10 +47,12 @@ void ConvertHttpResponseBodyToClientEncoding(struct HttpResponse *response) { char *body_latin1 = getLatin1FromUTF8(response->body); - strcpy(response->body, body_latin1); - checked_free(body_latin1); + strncpy(response->body, body_latin1, MAX_HTTP_BODY_SIZE); + response->body[MAX_HTTP_BODY_SIZE] = '\0'; response->body_size = strlen(response->body); + + checked_free(body_latin1); } static void SetHttpResponseToDefaults(struct HttpResponse *response) @@ -61,6 +65,22 @@ static void SetHttpResponseToDefaults(struct HttpResponse *response) response->status_text[0] = '\0'; } +struct HttpResponse *GetHttpResponseFromBuffer(void *buffer, int size) +{ + if (size > MAX_HTTP_BODY_SIZE) + return NULL; + + struct HttpResponse *response = checked_calloc(sizeof(struct HttpResponse)); + + SetHttpResponseToDefaults(response); + + strncpy(response->body, buffer, MAX_HTTP_BODY_SIZE); + response->body[MAX_HTTP_BODY_SIZE] = '\0'; + response->body_size = MIN(size, MAX_HTTP_BODY_SIZE); + + return response; +} + static boolean SetHTTPResponseCode(struct HttpResponse *response, char *buffer) { char *prefix = "HTTP/1.1 "; @@ -146,19 +166,22 @@ static boolean SetHTTPResponseBody(struct HttpResponse *response, char *buffer, return TRUE; } -boolean DoHttpRequest(struct HttpRequest *request, - struct HttpResponse *response) +static boolean DoHttpRequestExt(struct HttpRequest *request, + struct HttpResponse *response, + char *send_buffer, + char *recv_buffer, + int max_http_buffer_size, + SDLNet_SocketSet *socket_set, + TCPsocket *socket) { - SDLNet_SocketSet socket_set; - TCPsocket socket; IPaddress ip; int server_host; SetHttpResponseToDefaults(response); - socket_set = SDLNet_AllocSocketSet(1); + *socket_set = SDLNet_AllocSocketSet(1); - if (socket_set == NULL) + if (*socket_set == NULL) { SetHttpError("cannot allocate socket set"); @@ -182,9 +205,9 @@ boolean DoHttpRequest(struct HttpRequest *request, (server_host >> 8) & 0xff, (server_host >> 0) & 0xff); - socket = SDLNet_TCP_Open(&ip); + *socket = SDLNet_TCP_Open(&ip); - if (socket == NULL) + if (*socket == NULL) { SetHttpError("cannot connect to host '%s': %s", request->hostname, SDLNet_GetError()); @@ -192,7 +215,7 @@ boolean DoHttpRequest(struct HttpRequest *request, return FALSE; } - if (SDLNet_TCP_AddSocket(socket_set, socket) == -1) + if (SDLNet_TCP_AddSocket(*socket_set, *socket) == -1) { SetHttpError("cannot add socket to socket set"); @@ -213,17 +236,13 @@ boolean DoHttpRequest(struct HttpRequest *request, request->hostname, (int)strlen(request->body)); - int max_http_buffer_size = MAX_HTTP_HEAD_SIZE + MAX_HTTP_BODY_SIZE; - char send_buffer[max_http_buffer_size + 1]; - char recv_buffer[max_http_buffer_size + 1]; - snprintf(send_buffer, max_http_buffer_size, "%s\r\n%s", request->head, request->body); Debug("network:http", "client request:\n--- snip ---\n%s\n--- snip ---", send_buffer); - int send_bytes = SDLNet_TCP_Send(socket, send_buffer, strlen(send_buffer)); + int send_bytes = SDLNet_TCP_Send(*socket, send_buffer, strlen(send_buffer)); if (send_bytes != strlen(send_buffer)) { @@ -232,7 +251,7 @@ boolean DoHttpRequest(struct HttpRequest *request, return FALSE; } - int recv_bytes = SDLNet_TCP_Recv(socket, recv_buffer, max_http_buffer_size); + int recv_bytes = SDLNet_TCP_Recv(*socket, recv_buffer, max_http_buffer_size); if (recv_bytes <= 0) { @@ -267,12 +286,41 @@ boolean DoHttpRequest(struct HttpRequest *request, return FALSE; } - SDLNet_TCP_DelSocket(socket_set, socket); - SDLNet_TCP_Close(socket); - Debug("network:http", "server response: %d %s", response->status_code, response->status_text); return TRUE; } + +boolean DoHttpRequest(struct HttpRequest *request, + struct HttpResponse *response) +{ + int max_http_buffer_size = MAX_HTTP_HEAD_SIZE + MAX_HTTP_BODY_SIZE; + char *send_buffer = checked_malloc(max_http_buffer_size + 1); + char *recv_buffer = checked_malloc(max_http_buffer_size + 1); + SDLNet_SocketSet socket_set = NULL; + TCPsocket socket = NULL; + + boolean success = DoHttpRequestExt(request, response, + send_buffer, recv_buffer, + max_http_buffer_size, + &socket_set, &socket); + if (socket_set != NULL) + { + if (socket != NULL) + { + SDLNet_TCP_DelSocket(socket_set, socket); + SDLNet_TCP_Close(socket); + } + + SDLNet_FreeSocketSet(socket_set); + } + + checked_free(send_buffer); + checked_free(recv_buffer); + + runtime.use_api_server = success; + + return success; +}