1/*
2 This file is part of Mtproto-proxy Library.
3
4 Mtproto-proxy Library is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 Mtproto-proxy Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright 2010-2012 Vkontakte Ltd
18 2010-2012 Nikolai Durov
19 2010-2012 Andrey Lopatin
20 2012 Anton Maydell
21
22 Copyright 2014-2016 Telegram Messenger Inc
23 2015-2016 Vitaly Valtman
24*/
25
26#pragma once
27
28#include "net/net-connections.h"
29//#include "net/net-buffers.h"
30#define MAX_HTTP_HEADER_SIZE 16384
31
32struct http_server_functions {
33 void *info;
34 int (*execute)(connection_job_t c, struct raw_message *raw, int op); /* invoked from parse_execute() */
35 int (*ht_wakeup)(connection_job_t c);
36 int (*ht_alarm)(connection_job_t c);
37 int (*ht_close)(connection_job_t c, int who);
38};
39
40#define HTTP_V09 9
41#define HTTP_V10 0x100
42#define HTTP_V11 0x101
43
44/* in conn->custom_data, 104 bytes */
45struct hts_data {
46 int query_type;
47 int query_flags;
48 int query_words;
49 int header_size;
50 int first_line_size;
51 int data_size;
52 int host_offset;
53 int host_size;
54 int uri_offset;
55 int uri_size;
56 int http_ver;
57 int wlen;
58 char word[16];
59 void *extra;
60 int extra_int;
61 int extra_int2;
62 int extra_int3;
63 int extra_int4;
64 double extra_double, extra_double2;
65 int parse_state;
66 int query_seqno;
67};
68
69/* for hts_data.query_type */
70enum hts_query_type {
71 htqt_none,
72 htqt_head,
73 htqt_get,
74 htqt_post,
75 htqt_options,
76 htqt_error,
77 htqt_empty
78};
79
80#define QF_ERROR 1
81#define QF_HOST 2
82#define QF_DATASIZE 4
83#define QF_CONNECTION 8
84#define QF_TRANSFER_ENCODING 16
85#define QF_TRANSFER_ENCODING_CHUNKED 32
86#define QF_KEEPALIVE 0x100
87#define QF_EXTRA_HEADERS 0x200
88
89#define HTS_DATA(c) ((struct hts_data *) (CONN_INFO(c)->custom_data))
90#define HTS_FUNC(c) ((struct http_server_functions *) (CONN_INFO(c)->extra))
91
92extern conn_type_t ct_http_server;
93extern struct http_server_functions default_http_server;
94
95int hts_do_wakeup (connection_job_t c);
96int hts_parse_execute (connection_job_t c);
97int hts_std_wakeup (connection_job_t c);
98int hts_std_alarm (connection_job_t c);
99int hts_init_accepted (connection_job_t c);
100int hts_close_connection (connection_job_t c, int who);
101void http_flush (connection_job_t C, struct raw_message *raw);
102
103extern int http_connections;
104extern long long http_queries, http_bad_headers, http_queries_size;
105
106extern char *extra_http_response_headers;
107
108/* useful functions */
109int get_http_header (const char *qHeaders, const int qHeadersLen, char *buffer, int b_len, const char *arg_name, const int arg_len);
110
111#define HTTP_DATE_LEN 29
112void gen_http_date (char date_buffer[29], int time);
113int gen_http_time (char *date_buffer, int *time);
114char *cur_http_date (void);
115//int write_basic_http_header (connection_job_t c, int code, int date, int len, const char *add_header, const char *content_type);
116int write_basic_http_header_raw (connection_job_t c, struct raw_message *raw, int code, int date, int len, const char *add_header, const char *content_type);
117int write_http_error (connection_job_t c, int code);
118int write_http_error_raw (connection_job_t c, struct raw_message *raw, int code);
119
120/* END */
121