WIP some work on http req/response handling

This commit is contained in:
Stephen Seo 2024-09-02 13:51:10 +09:00
parent 0022bf8b39
commit 0b9f0e38f8
2 changed files with 85 additions and 2 deletions

View file

@ -19,14 +19,89 @@
// Standard library includes. // Standard library includes.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
// Local includes // Local includes
#include "constants.h" #include "constants.h"
#define REQUEST_TYPE_BUFFER_SIZE 16
#define REQUEST_PATH_BUFFER_SIZE 256
#define REQUEST_PROTO_BUFFER_SIZE 16
char *c_simple_http_request_response( char *c_simple_http_request_response(
const char *request, const char *request,
unsigned int size, unsigned int size,
const C_SIMPLE_HTTP_HTTPTemplates *templates) { const C_SIMPLE_HTTP_HTTPTemplates *templates) {
// parse first line.
unsigned int idx = 0;
char request_type[REQUEST_TYPE_BUFFER_SIZE] = {0};
unsigned int request_type_idx = 0;
for (; idx < size
&& request[idx] != ' '
&& request[idx] != '\n'
&& request[idx] != '\r'
&& request[idx] != '\t'
&& request_type_idx < REQUEST_TYPE_BUFFER_SIZE - 1;
++idx) {
request_type[request_type_idx++] = request[idx];
}
if (request_type_idx == 0) {
return NULL;
}
#ifndef NDEBUG
fprintf(stderr, "Parsing request: got type \"%s\"\n", request_type);
#endif
// skip whitespace until next part.
for (; idx < size
&& (request[idx] == ' ' ||
request[idx] == '\n' ||
request[idx] == '\r' ||
request[idx] == '\t');
++idx) {}
// parse request path.
char request_path[REQUEST_PATH_BUFFER_SIZE] = {0};
unsigned int request_path_idx = 0;
for (; idx < size
&& request[idx] != ' '
&& request[idx] != '\n'
&& request[idx] != '\r'
&& request[idx] != '\t'
&& request_path_idx < REQUEST_PATH_BUFFER_SIZE - 1;
++idx) {
request_path[request_path_idx++] = request[idx];
}
if (request_path_idx == 0) {
return NULL;
}
#ifndef NDEBUG
fprintf(stderr, "Parsing request: got path \"%s\"\n", request_path);
#endif
// skip whitespace until next part.
for (; idx < size
&& (request[idx] == ' ' ||
request[idx] == '\n' ||
request[idx] == '\r' ||
request[idx] == '\t');
++idx) {}
// parse request http protocol.
char request_proto[REQUEST_PROTO_BUFFER_SIZE] = {0};
unsigned int request_proto_idx = 0;
for (; idx < size
&& request[idx] != ' '
&& request[idx] != '\n'
&& request[idx] != '\r'
&& request[idx] != '\t'
&& request_proto_idx < REQUEST_PROTO_BUFFER_SIZE - 1;
++idx) {
request_proto[request_proto_idx++] = request[idx];
}
if (request_proto_idx == 0) {
return NULL;
}
#ifndef NDEBUG
fprintf(stderr, "Parsing request: got http protocol \"%s\"\n", request_proto);
#endif
// TODO // TODO
return NULL; return NULL;
} }

View file

@ -34,6 +34,7 @@
#include "signal_handling.h" #include "signal_handling.h"
#include "globals.h" #include "globals.h"
#include "constants.h" #include "constants.h"
#include "http.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
Args args = parse_args(argc, argv); Args args = parse_args(argc, argv);
@ -83,17 +84,24 @@ int main(int argc, char **argv) {
int connection_fd = ret; int connection_fd = ret;
read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE); read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE);
// DEBUG print received buf. // DEBUG print received buf.
// TODO Validate request and send response.
for (unsigned int idx = 0; for (unsigned int idx = 0;
idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret; idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret;
++idx) { ++idx) {
if (recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E) { if ((recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E)
|| recv_buf[idx] == '\n' || recv_buf[idx] == '\r') {
printf("%c", recv_buf[idx]); printf("%c", recv_buf[idx]);
} else { } else {
break; break;
} }
} }
puts(""); puts("");
// TODO Validate request and send response.
// TODO WIP
const char *response = c_simple_http_request_response(
(const char*)recv_buf, read_ret, NULL);
if (response) {
free((void*)response);
}
close(connection_fd); close(connection_fd);
} else { } else {
printf("WARNING: accept: Unknown invalid state!\n"); printf("WARNING: accept: Unknown invalid state!\n");