2022-06-30 21:22:42 +02:00
|
|
|
#ifndef HTTP_PARSER_H
|
|
|
|
#define HTTP_PARSER_H
|
|
|
|
|
2023-12-18 00:02:58 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* http_parser.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-08-22 22:16:32 +02:00
|
|
|
#include "core/containers/vector.h"
|
2023-02-19 10:34:39 +01:00
|
|
|
#include "core/string/ustring.h"
|
2024-03-09 23:39:33 +01:00
|
|
|
#include "core/os/file_access.h"
|
2022-06-30 23:12:32 +02:00
|
|
|
|
2022-08-17 13:45:14 +02:00
|
|
|
#include "core/object/reference.h"
|
2022-06-30 21:22:42 +02:00
|
|
|
|
2024-03-09 23:39:33 +01:00
|
|
|
#include "web_server_simple.h"
|
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
class SimpleWebServerRequest;
|
2022-07-01 01:03:23 +02:00
|
|
|
struct http_parser;
|
|
|
|
struct http_parser_settings;
|
2022-08-22 22:16:32 +02:00
|
|
|
struct multipart_parser;
|
|
|
|
struct multipart_parser_settings;
|
2022-06-30 23:12:32 +02:00
|
|
|
|
2022-06-30 21:22:42 +02:00
|
|
|
class HTTPParser : public Reference {
|
2022-06-30 23:12:32 +02:00
|
|
|
GDCLASS(HTTPParser, Reference);
|
|
|
|
|
2022-06-30 21:22:42 +02:00
|
|
|
public:
|
2022-07-20 16:28:37 +02:00
|
|
|
enum HTTPRequestContentType {
|
|
|
|
REQUEST_CONTENT_URLENCODED = 0,
|
|
|
|
REQUEST_CONTENT_MULTIPART_FORM_DATA,
|
|
|
|
REQUEST_CONTENT_TEXT_PLAIN,
|
|
|
|
};
|
|
|
|
|
2023-03-18 11:52:25 +01:00
|
|
|
uint64_t max_request_size;
|
2024-03-09 23:39:33 +01:00
|
|
|
uint64_t request_max_file_upload_size;
|
|
|
|
|
|
|
|
WebServerSimple::FileUploadStoreType upload_file_store_type;
|
|
|
|
String upload_temp_file_store_path;
|
2023-03-18 11:52:25 +01:00
|
|
|
|
2023-02-19 10:34:39 +01:00
|
|
|
Ref<SimpleWebServerRequest> get_next_request();
|
2022-07-01 17:40:47 +02:00
|
|
|
int get_request_count() const;
|
2022-06-30 23:12:32 +02:00
|
|
|
|
|
|
|
bool is_ready() const;
|
2022-07-01 18:25:45 +02:00
|
|
|
bool is_finished() const;
|
2023-02-19 10:34:39 +01:00
|
|
|
bool has_error() const;
|
2022-06-30 23:12:32 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-06-30 21:22:42 +02:00
|
|
|
HTTPParser();
|
|
|
|
~HTTPParser();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
2022-06-30 23:12:32 +02:00
|
|
|
|
|
|
|
String _partial_data;
|
|
|
|
|
2023-03-18 11:52:25 +01:00
|
|
|
uint64_t _current_request_size;
|
2024-03-09 23:39:33 +01:00
|
|
|
uint64_t _current_upload_files_size;
|
|
|
|
|
|
|
|
String _upload_file_full_path;
|
|
|
|
FileAccess *_upload_file_access;
|
2023-03-18 11:52:25 +01:00
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
Ref<SimpleWebServerRequest> _request;
|
|
|
|
Vector<Ref<SimpleWebServerRequest>> _requests;
|
2022-06-30 23:12:32 +02:00
|
|
|
|
|
|
|
bool _is_ready;
|
2022-07-01 01:03:23 +02:00
|
|
|
|
2022-08-22 22:16:32 +02:00
|
|
|
HTTPRequestContentType _content_type;
|
|
|
|
bool _in_header;
|
|
|
|
String _queued_header_field;
|
|
|
|
|
|
|
|
String _multipart_boundary;
|
|
|
|
|
|
|
|
String _queued_multipart_header_field;
|
|
|
|
Vector<char> _queued_multipart_form_data;
|
|
|
|
|
|
|
|
String _multipart_form_name;
|
|
|
|
String _multipart_form_filename;
|
|
|
|
String _multipart_form_content_type;
|
|
|
|
bool _multipart_form_is_file;
|
|
|
|
Vector<char> _multipart_form_data;
|
|
|
|
|
2023-02-19 10:34:39 +01:00
|
|
|
bool _error;
|
|
|
|
|
2022-07-01 01:03:23 +02:00
|
|
|
private:
|
2022-08-22 22:16:32 +02:00
|
|
|
int process_multipart_data(const char *at, size_t length);
|
|
|
|
void _process_multipart_header_value(const String &val);
|
2022-07-21 00:28:57 +02:00
|
|
|
void process_urlenc_data();
|
2022-08-22 22:16:32 +02:00
|
|
|
bool is_boundary_at(const char *at, size_t length);
|
2022-07-20 21:29:59 +02:00
|
|
|
|
2022-07-01 01:03:23 +02:00
|
|
|
int on_message_begin();
|
|
|
|
int on_url(const char *at, size_t length);
|
|
|
|
int on_status(const char *at, size_t length);
|
|
|
|
int on_header_field(const char *at, size_t length);
|
|
|
|
int on_header_value(const char *at, size_t length);
|
|
|
|
int on_headers_complete();
|
|
|
|
int on_body(const char *at, size_t length);
|
|
|
|
int on_message_complete();
|
|
|
|
int on_chunk_header();
|
|
|
|
int on_chunk_complete();
|
|
|
|
|
|
|
|
static int _on_message_begin_cb(http_parser *parser);
|
|
|
|
static int _on_url_cb(http_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_status_cb(http_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_header_field_cb(http_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_header_value_cb(http_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_headers_complete_cb(http_parser *parser);
|
|
|
|
static int _on_body_cb(http_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_message_complete_cb(http_parser *parser);
|
|
|
|
static int _on_chunk_header_cb(http_parser *parser);
|
|
|
|
static int _on_chunk_complete_cb(http_parser *parser);
|
|
|
|
|
2022-08-22 22:16:32 +02:00
|
|
|
int on_multipart_header_field_cb(const char *at, size_t length);
|
|
|
|
int on_multipart_header_value_cb(const char *at, size_t length);
|
|
|
|
int on_multipart_part_data_cb(const char *at, size_t length);
|
|
|
|
int on_multipart_part_data_begin_cb();
|
|
|
|
int on_multipart_headers_complete_cb();
|
|
|
|
int on_multipart_part_data_end_cb();
|
|
|
|
int on_multipart_body_end_cb();
|
|
|
|
|
|
|
|
static int _on_multipart_header_field_cb(multipart_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_multipart_header_value_cb(multipart_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_multipart_part_data_cb(multipart_parser *parser, const char *at, size_t length);
|
|
|
|
static int _on_multipart_part_data_begin_cb(multipart_parser *parser);
|
|
|
|
static int _on_multipart_headers_complete_cb(multipart_parser *parser);
|
|
|
|
static int _on_multipart_part_data_end_cb(multipart_parser *parser);
|
|
|
|
static int _on_multipart_body_end_cb(multipart_parser *parser);
|
|
|
|
|
2022-07-01 01:03:23 +02:00
|
|
|
http_parser *parser;
|
|
|
|
http_parser_settings *settings;
|
2022-07-01 17:40:47 +02:00
|
|
|
|
2022-08-22 22:16:32 +02:00
|
|
|
multipart_parser *_multipart_parser;
|
|
|
|
multipart_parser_settings *_multipart_parser_settings;
|
2022-06-30 21:22:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|