backend: add parser char skipper
This commit is contained in:
parent
02d5fa4b8e
commit
a30ee9d249
@ -1,26 +1,55 @@
|
|||||||
#include "http.h"
|
#include "http.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char* message;
|
||||||
|
size_t message_size;
|
||||||
|
size_t i;
|
||||||
|
} HttpRequestHeaderParser;
|
||||||
|
|
||||||
|
HttpMethod parse_method(HttpRequestHeaderParser* parser)
|
||||||
|
{
|
||||||
|
if (parser->i + 3 < parser->message_size && strncmp(&parser->message[0], "GET", 3)) {
|
||||||
|
parser->i += 3;
|
||||||
|
return HttpMethodGet;
|
||||||
|
} else if (parser->i + 4 < parser->message_size && strncmp(&parser->message[0], "POST", 4)) {
|
||||||
|
parser->i += 4;
|
||||||
|
return HttpMethodPost;
|
||||||
|
} else {
|
||||||
|
printf("error: http parser: failed to parse http method\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skip_char(HttpRequestHeaderParser* parser, char value)
|
||||||
|
{
|
||||||
|
if (parser->message[parser->i] != value) {
|
||||||
|
printf("error: http parser: unexpected character, expected ' '\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
parser->i += 1;
|
||||||
|
if (parser->i >= parser->message_size) {
|
||||||
|
printf("error: http parser: unexpected end\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t begin, length;
|
||||||
|
} PathSpan;
|
||||||
|
|
||||||
HttpRequestHeader parse_http_request_header(const char* message, size_t message_size)
|
HttpRequestHeader parse_http_request_header(const char* message, size_t message_size)
|
||||||
{
|
{
|
||||||
size_t i = 0;
|
HttpRequestHeaderParser parser = (HttpRequestHeaderParser) {
|
||||||
|
.message = message,
|
||||||
// parse method
|
.message_size = message_size,
|
||||||
HttpMethod method;
|
.i = 0,
|
||||||
if (i + 3 < message_size && strncmp(&message[0], "GET", 3)) {
|
};
|
||||||
method = HttpMethodGet;
|
HttpMethod method = parse_method(&parser);
|
||||||
i += 3;
|
skip_char(&parser, ' ');
|
||||||
} else if (i + 4 < message_size && strncmp(&message[0], "POST", 4)) {
|
|
||||||
method = HttpMethodPost;
|
|
||||||
i += 4;
|
|
||||||
} else {
|
|
||||||
printf("error: header parse fail #1\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// skip space
|
|
||||||
i += 1;
|
|
||||||
if (i >= message_size) { }
|
|
||||||
|
|
||||||
return (HttpRequestHeader) { 0 };
|
return (HttpRequestHeader) { 0 };
|
||||||
}
|
}
|
||||||
|
28
backend/http_request_example.txt
Normal file
28
backend/http_request_example.txt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
starting server...
|
||||||
|
listening on port 8000
|
||||||
|
waiting for client...
|
||||||
|
client connected
|
||||||
|
recieved:
|
||||||
|
GET / HTTP/1.1
|
||||||
|
Host: localhost:8000
|
||||||
|
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109
|
||||||
|
.0
|
||||||
|
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/w
|
||||||
|
ebp,*/*;q=0.8
|
||||||
|
Accept-Language: en-US,en;q=0.5
|
||||||
|
Accept-Encoding: gzip, deflate, br
|
||||||
|
Connection: keep-alive
|
||||||
|
Upgrade-Insecure-Requests: 1
|
||||||
|
Sec-Fetch-Dest: document
|
||||||
|
Sec-Fetch-Mode: navigate
|
||||||
|
Sec-Fetch-Site: none
|
||||||
|
Sec-Fetch-User: ?1
|
||||||
|
|
||||||
|
|
||||||
|
disconnecting client
|
||||||
|
error: could not recieve
|
||||||
|
waiting for client...
|
||||||
|
client connected
|
||||||
|
client disconnected
|
||||||
|
waiting for client...
|
||||||
|
|
Loading…
Reference in New Issue
Block a user