message_http.h
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_PROTOCOL_MESSAGE_HTTP_H__
#define __TR_PROTOCOL_MESSAGE_HTTP_H__
#include <sys/types.h>
#include "trbase.h"
#include "trio.h"
#include "trcomm.h"
#include "tr/proto_message.h"
#include "tr/protocol/http/header.h"
#include "tr/protocol/http/data_chunk.h"
#define HTTP_EXTMETH 0x00U
#define HTTP_RESPONSE 0x01U
#define HTTP_GET 0x02U
#define HTTP_HEAD 0x04U
#define HTTP_POST 0x08U
#define HTTP_PUT 0x10U
#define HTTP_DELETE 0x20U
#define HTTP_TRACE 0x40U
#define HTTP_CONNECT 0x80U
extern const char * const TR_http_methods[];
#define TR_IS_SET(x, n) ((n) == ((x) & (n))) // this is a more common thing and
// may be moved somewhere...
#define TR_HTTP_MSG_IS_REQ(message_p) ((message_p)->type != HTTP_RESPONSE)
#define TR_HTTP_MSG_HEADER(message_p, key) \
(httpHeaderGetValue(&((message_p)->headers), (key)))
#define TR_HTTP_MSG_HEADER_BY_IDX(message_p, _idx) \
((message_p)->headers.idx[_idx]->header)
#define TR_HTTP_MSG_TYPE_ID(message_p) ((message_p)->type)
#define TR_HTTP_MSG_VER_MAJ(message_p) ((message_p)->version.major)
#define TR_HTTP_MSG_VER_MIN(message_p) ((message_p)->version.minor)
#define TR_HTTP_MSG_BODY_SIZE(message_p) ((message_p)->_body_size)
#define TR_HTTP_MSG_IS_READY(message_p) ((message_p)->_status == READY)
#define TR_HTTP_MSG_REQ(message_p, field) \
(TR_HTTP_MSG_IS_REQ(message_p) ? (message_p)->info.request.field : NULL)
#define TR_HTTP_MSG_SET_HEADER(message_p, idx, value) \
httpHeaderStoreByIdx(&((message_p)->headers), (idx), (value))
#define TR_HTTP_MSG_SET_CUSTOM_HEADER(message_p, key, value) \
httpHeaderStore(&((message_p)->headers), (key), (value))
#define TR_HTTP_REQ_METHOD(message_p) (TR_HTTP_MSG_REQ((message_p), method))
#define TR_HTTP_REQ_URI(message_p) (TR_HTTP_MSG_REQ((message_p), uri))
#define TR_HTTP_RES_STATUS(message_p) \
(TR_HTTP_MSG_IS_REQ(message_p) ? -1 : (message_p)->info.response.status)
#define TR_HTTP_RES_MESSAGE(message_p) \
(TR_HTTP_MSG_IS_REQ(message_p) ? NULL : (message_p)->info.response.message)
#define TR_HTTP_MSG_DATA_CHUNK_SIZE 2048
typedef enum {
TR_GARBAGE=0,
TR_TYPE,
TR_HEAD2,
TR_HEAD3,
TR_HEADER,
TR_BODY,
TR_READY
} TR_HttpMsgStatus_e;
typedef enum {TR_KEEP_ALIVE, TR_CLOSE} TR_HttpMessageClose_e;
TR_CLASS(TR_ProtoMsgHttp) {
TR_EXTENDS(TR_ProtoMessage);
TR_HttpMsgStatus_e _status;
TR_HttpDataChunk _data;
size_t _body_size;
size_t _body_retrieved;
int _chunked;
size_t _chunk_remains;
HttpMessageClose_e _close;
unsigned char type;
struct {
unsigned int major;
unsigned int minor;
} version;
union {
struct {
char * method;
char * uri;
// char * uri_str;
// struct {
// char * scheme;
// char * host;
// char * path;
// } uri;
} request;
struct {
unsigned int status;
char * message;
} response;
} info;
TR_Hash headers;
TR_HttpHeader headers_idx[TR_N_HTTP_HEADER_IDX];
HttpDataChunk _body_chunks;
size_t _body_chunk_ofs;
};
TR_INSTANCE_INIT(TR_ProtoMsgHttp);
TR_CLASSVARS_DECL(TR_ProtoMsgHttp) {};
char * TR_httpMsgBody(TR_ProtoMsgHttp, char *);
size_t TR_httpMsgSize(TR_ProtoMsgHttp);
void TR_httpMsgSetHeader(TR_ProtoMsgHttp, unsigned int, const char *);
void TR_httpMsgSetCustomHeader(TR_ProtoMsgHttp, const char *, const char *);
void TR_httpMsgHeaderDelete(TR_ProtoMsgHttp, const char * const);
char * TR_httpMsgHeaderGetValue(TR_ProtoMsgHttp, const char * const);
void TR_httpMsgHeaderDestroy(TR_ProtoMsgHttp);
TR_HttpHeader TR_httpMsgHeaderStore(
TR_ProtoMsgHttp, const char * const, char * const);
TR_HttpHeader TR_httpMsgHeaderStoreByIdx(
TR_ProtoMsgHttp, unsigned int, char * const);
TR_HttpHeader TR_httpMsgHeaderGet(
TR_ProtoMsgHttp, const char * const);
#endif // __TR_PROTOCOL_MESSAGE_HTTP_H__
// vim: set ts=4 sw=4: