Update Iot.js
[platform/upstream/iotjs.git] / tools / src / module / iotjs_module_httpparser.h
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 #ifndef IOTJS_MODULE_HTTPPARSER_H
18 #define IOTJS_MODULE_HTTPPARSER_H
19
20
21 #include "iotjs_objectwrap.h"
22
23 #include "http_parser.h"
24
25
26 // If # of header fields == HEADER_MAX, flush header to JS side.
27 // This is weired : # of maximum headers in C equals to HEADER_MAX-1.
28 // This is because , OnHeaders cb, we increase n_fields first,
29 // and check whether field == HEADER_MAX.
30 // ex) HEADER_MAX 2 means that we can keep at most 1 header field/value
31 // during http parsing.
32 // Increase this to minimize inter JS-C call
33 #define HEADER_MAX 10
34
35
36 typedef struct {
37   iotjs_jobjectwrap_t jobjectwrap;
38
39   http_parser parser;
40
41   iotjs_string_t url;
42   iotjs_string_t status_msg;
43
44   iotjs_string_t fields[HEADER_MAX];
45   iotjs_string_t values[HEADER_MAX];
46   size_t n_fields;
47   size_t n_values;
48
49   iotjs_jval_t* cur_jbuf;
50   char* cur_buf;
51   size_t cur_buf_len;
52
53   bool flushed;
54 } IOTJS_VALIDATED_STRUCT(iotjs_httpparserwrap_t);
55
56
57 typedef enum http_parser_type http_parser_type;
58
59
60 #define THIS iotjs_httpparserwrap_t* httpparserwrap
61
62
63 iotjs_httpparserwrap_t* iotjs_httpparserwrap_create(const iotjs_jval_t* jparser,
64                                                     http_parser_type type);
65
66 void iotjs_httpparserwrap_initialize(THIS, http_parser_type type);
67 iotjs_jval_t iotjs_httpparserwrap_make_header(THIS);
68
69 void iotjs_httpparserwrap_flush(THIS);
70
71 void iotjs_httpparserwrap_set_buf(THIS, iotjs_jval_t* jbuf, char* buf, int sz);
72
73 iotjs_jval_t* iotjs_httpparserwrap_jobject(THIS);
74 http_parser* iotjs_httpparserwrap_parser(THIS);
75
76
77 #undef THIS
78
79
80 #endif /* IOTJS_MODULE_HTTPPARSER_H */