1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2021 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
20 static char http_error[MAX_HTTP_ERROR_SIZE];
22 static void SetHttpError(char *format, ...)
27 vsnprintf(http_error, MAX_HTTP_ERROR_SIZE, format, ap);
31 char *GetHttpError(void)
36 void ConvertHttpRequestBodyToServerEncoding(struct HttpRequest *request)
38 char *body_utf8 = getUTF8FromLatin1(request->body);
40 strcpy(request->body, body_utf8);
41 checked_free(body_utf8);
44 void ConvertHttpResponseBodyToClientEncoding(struct HttpResponse *response)
46 char *body_latin1 = getLatin1FromUTF8(response->body);
48 strcpy(response->body, body_latin1);
49 checked_free(body_latin1);
51 response->body_size = strlen(response->body);
54 static void SetHttpResponseToDefaults(struct HttpResponse *response)
56 response->head[0] = '\0';
57 response->body[0] = '\0';
58 response->body_size = 0;
60 response->status_code = 0;
61 response->status_text[0] = '\0';
64 static boolean SetHTTPResponseCode(struct HttpResponse *response, char *buffer)
66 char *prefix = "HTTP/1.1 ";
67 char *prefix_start = strstr(buffer, prefix);
69 if (prefix_start == NULL)
72 char *status_code_start = prefix_start + strlen(prefix);
73 char *status_code_end = strstr(status_code_start, " ");
75 if (status_code_end == NULL)
78 int status_code_size = status_code_end - status_code_start;
80 if (status_code_size != 3) // status code must have three digits
83 char status_code[status_code_size + 1];
85 strncpy(status_code, status_code_start, status_code_size);
86 status_code[status_code_size] = '\0';
88 response->status_code = atoi(status_code);
90 char *status_text_start = status_code_end + 1;
91 char *status_text_end = strstr(status_text_start, "\r\n");
93 if (status_text_end == NULL)
96 int status_text_size = status_text_end - status_text_start;
98 if (status_text_size > MAX_HTTP_ERROR_SIZE)
101 strncpy(response->status_text, status_text_start, status_text_size);
102 response->status_text[status_text_size] = '\0';
107 static boolean SetHTTPResponseHead(struct HttpResponse *response, char *buffer)
109 char *separator = "\r\n\r\n";
110 char *separator_start = strstr(buffer, separator);
112 if (separator_start == NULL)
115 int head_size = separator_start - buffer;
117 if (head_size > MAX_HTTP_HEAD_SIZE)
120 strncpy(response->head, buffer, head_size);
121 response->head[head_size] = '\0';
126 static boolean SetHTTPResponseBody(struct HttpResponse *response, char *buffer,
129 char *separator = "\r\n\r\n";
130 char *separator_start = strstr(buffer, separator);
132 if (separator_start == NULL)
135 int separator_size = strlen(separator);
136 int full_head_size = separator_start + separator_size - buffer;
137 int body_size = buffer_size - full_head_size;
139 if (body_size > MAX_HTTP_BODY_SIZE)
142 memcpy(response->body, buffer + full_head_size, body_size);
143 response->body[body_size] = '\0';
144 response->body_size = body_size;
149 boolean DoHttpRequest(struct HttpRequest *request,
150 struct HttpResponse *response)
152 SDLNet_SocketSet socket_set;
157 SetHttpResponseToDefaults(response);
159 socket_set = SDLNet_AllocSocketSet(1);
161 if (socket_set == NULL)
163 SetHttpError("cannot allocate socket set");
168 SDLNet_ResolveHost(&ip, request->hostname, request->port);
170 if (ip.host == INADDR_NONE)
172 SetHttpError("cannot resolve hostname '%s'", request->hostname);
177 server_host = SDLNet_Read32(&ip.host);
179 Debug("network:http", "trying to connect to server at %d.%d.%d.%d ...",
180 (server_host >> 24) & 0xff,
181 (server_host >> 16) & 0xff,
182 (server_host >> 8) & 0xff,
183 (server_host >> 0) & 0xff);
185 socket = SDLNet_TCP_Open(&ip);
189 SetHttpError("cannot connect to host '%s': %s", request->hostname,
195 if (SDLNet_TCP_AddSocket(socket_set, socket) == -1)
197 SetHttpError("cannot add socket to socket set");
202 Debug("network:http", "successfully connected to server");
204 snprintf(request->head, MAX_HTTP_HEAD_SIZE,
207 "X-Requested-With: XMLHttpRequest\r\n"
208 "Content-Type: application/json\r\n"
209 "Connection: close\r\n"
210 "Content-Length: %d\r\n",
214 (int)strlen(request->body));
216 int max_http_buffer_size = MAX_HTTP_HEAD_SIZE + MAX_HTTP_BODY_SIZE;
217 char send_buffer[max_http_buffer_size + 1];
218 char recv_buffer[max_http_buffer_size + 1];
220 snprintf(send_buffer, max_http_buffer_size,
221 "%s\r\n%s", request->head, request->body);
223 Debug("network:http", "client request:\n--- snip ---\n%s\n--- snip ---",
226 int send_bytes = SDLNet_TCP_Send(socket, send_buffer, strlen(send_buffer));
228 if (send_bytes != strlen(send_buffer))
230 SetHttpError("sending request to server failed");
235 int recv_bytes = SDLNet_TCP_Recv(socket, recv_buffer, max_http_buffer_size);
239 SetHttpError("receiving response from server failed");
244 recv_buffer[recv_bytes] = '\0';
246 Debug("network:http", "server response:\n--- snip ---\n%s\n--- snip ---",
249 if (!SetHTTPResponseCode(response, recv_buffer))
251 SetHttpError("malformed HTTP response");
256 if (!SetHTTPResponseHead(response, recv_buffer))
258 SetHttpError("invalid HTTP response header");
263 if (!SetHTTPResponseBody(response, recv_buffer, recv_bytes))
265 SetHttpError("invalid HTTP response body");
270 SDLNet_TCP_DelSocket(socket_set, socket);
271 SDLNet_TCP_Close(socket);
273 Debug("network:http", "server response: %d %s",
274 response->status_code,
275 response->status_text);