feat: start of http part of server
This commit is contained in:
parent
6d45ec59dc
commit
c58e53eb2a
7 changed files with 91 additions and 29 deletions
11
src/http/http_consts.c
Normal file
11
src/http/http_consts.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include "http.h"
|
||||
|
||||
const char http_404[] = "HTTP/1.1 404 Not Found\n"
|
||||
"Connection: close\n"
|
||||
"Content-Length: 0\n\n";
|
||||
const size_t http_404_len = sizeof(http_404) - 1;
|
||||
|
||||
const char http_500[] = "HTTP/1.1 500 Internal Server Error\n"
|
||||
"Connection: close\n"
|
||||
"Content-Length: 0\n\n";
|
||||
const size_t http_500_len = sizeof(http_500) - 1;
|
||||
30
src/http/http_parse.c
Normal file
30
src/http/http_parse.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include "http.h"
|
||||
#include "picohttpparser.h"
|
||||
|
||||
/*
|
||||
* given the HTTP path, parse the request
|
||||
*/
|
||||
http_parse_error http_parse_request(http_request *req, const char *buf, size_t buf_size) {
|
||||
// First we try to parse the incoming HTTP request
|
||||
const char *method, *path;
|
||||
struct phr_header headers[16];
|
||||
size_t method_len, path_len, num_headers;
|
||||
int minor_version;
|
||||
|
||||
int res = phr_parse_request(buf, buf_size,
|
||||
&method, &method_len, &path, &path_len,
|
||||
&minor_version, headers, &num_headers, 0);
|
||||
|
||||
if (res == -2) {
|
||||
return http_parse_error_incomplete;
|
||||
} else if (res < 0) {
|
||||
return http_parse_error_invalid;
|
||||
}
|
||||
|
||||
// Next, we parse the HTTP request as a lander-specific request
|
||||
if (path_len == 0 || path[0] != '/') {
|
||||
return http_parse_error_invalid;
|
||||
}
|
||||
|
||||
return http_parse_error_ok;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue