mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2024-11-22 17:07:20 +01:00
Some initial work on the parser.
This commit is contained in:
parent
ddf3dae42b
commit
08bb253a5c
@ -1,6 +1,41 @@
|
||||
#include "http_parser.h"
|
||||
|
||||
#include "web_server_request.h"
|
||||
|
||||
Ref<WebServerRequest> HTTPParser::get_request() {
|
||||
return _request;
|
||||
}
|
||||
void HTTPParser::set_request(const Ref<WebServerRequest> &val) {
|
||||
_request = val;
|
||||
}
|
||||
|
||||
bool HTTPParser::is_ready() const {
|
||||
return _is_ready;
|
||||
}
|
||||
|
||||
void HTTPParser::reset() {
|
||||
_partial_data = "";
|
||||
_is_ready = false;
|
||||
}
|
||||
|
||||
//returns the index where processing was ended -> start of the next query if != data_length
|
||||
int HTTPParser::read_from_buffer(const char *p_buffer, const int p_data_length) {
|
||||
ERR_FAIL_COND_V(!_request.is_valid(), p_data_length);
|
||||
|
||||
int current_buffer_index = 0;
|
||||
|
||||
_partial_data.resize(p_data_length - current_buffer_index);
|
||||
|
||||
CharType *pdp = _partial_data.ptrw();
|
||||
for (int i = 0; i < current_buffer_index; ++i) {
|
||||
pdp[i] = p_buffer[i + current_buffer_index];
|
||||
}
|
||||
|
||||
return current_buffer_index;
|
||||
}
|
||||
|
||||
HTTPParser::HTTPParser() {
|
||||
_is_ready = false;
|
||||
}
|
||||
|
||||
HTTPParser::~HTTPParser() {
|
||||
|
@ -1,15 +1,37 @@
|
||||
#ifndef HTTP_PARSER_H
|
||||
#define HTTP_PARSER_H
|
||||
|
||||
#include "core/ustring.h"
|
||||
|
||||
#include "core/reference.h"
|
||||
|
||||
class WebServerRequest;
|
||||
|
||||
class HTTPParser : public Reference {
|
||||
GDCLASS(HTTPParser, Reference);
|
||||
|
||||
public:
|
||||
Ref<WebServerRequest> get_request();
|
||||
void set_request(const Ref<WebServerRequest> &val);
|
||||
|
||||
bool is_ready() const;
|
||||
|
||||
void reset();
|
||||
|
||||
//returns the index where processing was ended -> start of the next query if != data_length
|
||||
int read_from_buffer(const char *p_buffer, const int p_data_length);
|
||||
|
||||
HTTPParser();
|
||||
~HTTPParser();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
String _partial_data;
|
||||
|
||||
Ref<WebServerRequest> _request;
|
||||
|
||||
bool _is_ready;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include "core/reference.h"
|
||||
|
||||
class HTTPWriter : public Reference {
|
||||
GDCLASS(HTTPWriter, Reference);
|
||||
|
||||
public:
|
||||
HTTPWriter();
|
||||
~HTTPWriter();
|
||||
|
@ -30,6 +30,9 @@
|
||||
|
||||
#include "http_server_simple.h"
|
||||
|
||||
#include "../http/http_parser.h"
|
||||
#include "simple_web_server_request.h"
|
||||
|
||||
void HTTPServerSimple::stop() {
|
||||
server->stop();
|
||||
_clear_client();
|
||||
@ -166,27 +169,48 @@ void HTTPServerSimple::poll() {
|
||||
}
|
||||
}
|
||||
|
||||
//char *r = (char *)req_buf;
|
||||
//int l = req_pos - 1;
|
||||
//if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
|
||||
// _send_response();
|
||||
// _clear_client();
|
||||
// return;
|
||||
// }
|
||||
|
||||
int read = 0;
|
||||
Error err = peer->get_partial_data(req_buf, 4096, read);
|
||||
|
||||
if (err != OK) {
|
||||
// Got an error
|
||||
_clear_client();
|
||||
return;
|
||||
}
|
||||
|
||||
if (read == 0) {
|
||||
// Busy, wait next poll
|
||||
return;
|
||||
}
|
||||
|
||||
int buffer_start_index = 0;
|
||||
while (true) {
|
||||
char *r = (char *)req_buf;
|
||||
int l = req_pos - 1;
|
||||
if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
|
||||
_send_response();
|
||||
_clear_client();
|
||||
return;
|
||||
char *rb = reinterpret_cast<char *>(&req_buf[buffer_start_index]);
|
||||
buffer_start_index = _http_parser->read_from_buffer(rb, read);
|
||||
|
||||
if (_http_parser->is_ready()) {
|
||||
Ref<SimpleWebServerRequest> req;
|
||||
|
||||
req = _http_parser->get_request();
|
||||
|
||||
//handle request
|
||||
|
||||
req.instance();
|
||||
_http_parser->set_request(req);
|
||||
_http_parser->reset();
|
||||
}
|
||||
|
||||
int read = 0;
|
||||
ERR_FAIL_COND(req_pos >= 4096);
|
||||
Error err = peer->get_partial_data(&req_buf[req_pos], 1, read);
|
||||
if (err != OK) {
|
||||
// Got an error
|
||||
_clear_client();
|
||||
return;
|
||||
} else if (read != 1) {
|
||||
// Busy, wait next poll
|
||||
if (buffer_start_index == read) {
|
||||
return;
|
||||
}
|
||||
req_pos += read;
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,6 +223,10 @@ HTTPServerSimple::HTTPServerSimple() {
|
||||
mimes["svg"] = "image/svg";
|
||||
mimes["wasm"] = "application/wasm";
|
||||
server.instance();
|
||||
_http_parser.instance();
|
||||
Ref<SimpleWebServerRequest> req;
|
||||
req.instance();
|
||||
_http_parser->set_request(req);
|
||||
stop();
|
||||
}
|
||||
|
||||
@ -208,7 +236,6 @@ void HTTPServerSimple::_clear_client() {
|
||||
tcp = Ref<StreamPeerTCP>();
|
||||
memset(req_buf, 0, sizeof(req_buf));
|
||||
time = 0;
|
||||
req_pos = 0;
|
||||
}
|
||||
|
||||
void HTTPServerSimple::_set_internal_certs(Ref<Crypto> p_crypto) {
|
||||
|
@ -37,6 +37,8 @@
|
||||
|
||||
#include "core/project_settings.h"
|
||||
|
||||
class HTTPParser;
|
||||
|
||||
class HTTPServerSimple : public Reference {
|
||||
public:
|
||||
void stop();
|
||||
@ -56,10 +58,10 @@ private:
|
||||
Ref<StreamPeer> peer;
|
||||
Ref<CryptoKey> key;
|
||||
Ref<X509Certificate> cert;
|
||||
Ref<HTTPParser> _http_parser;
|
||||
bool use_ssl = false;
|
||||
uint64_t time = 0;
|
||||
uint8_t req_buf[4096];
|
||||
int req_pos = 0;
|
||||
|
||||
void _clear_client();
|
||||
void _set_internal_certs(Ref<Crypto> p_crypto);
|
||||
|
Loading…
Reference in New Issue
Block a user