-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththttp.h
More file actions
153 lines (118 loc) · 4.33 KB
/
thttp.h
File metadata and controls
153 lines (118 loc) · 4.33 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
/*
* Copyright (c) 2017-2021 Pantacor Ltd.
*
* 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.
*/
#ifndef THTTP_H
#define THTTP_H
#include "thttp-enums.h"
#include <netinet/in.h>
#include <stdarg.h>
#include <sys/types.h>
#ifndef DEBUG
#define DEBUG 0
#endif
#ifndef VERBOSE
#define VERBOSE 0
#endif
typedef enum {
LOG_FATAL, // 0
LOG_ERROR, // 1
LOG_WARN, // 2
LOG_INFO, // 3
LOG_DEBUG, // 4
LOG_ALL // 5
} thttp_log_level_t ;
typedef struct thttp_request {
thttp_method_t method;
thttp_proto_t proto;
thttp_proto_version_t proto_version;
int is_tls;
int proxyconnect;
char *host;
int port;
char *host_proxy;
int port_proxy;
const char *user_agent;
char *baseurl;
char *path;
char **headers;
int nr_headers;
int fd;
off_t len;
char *body;
char *body_content_type;
struct sockaddr conn;
} thttp_request_t;
// super struct for tls requests. ensure your thttp_request_t file
// has is_tls set.
// if you want thttp to ignore certificate problems you need to keep
// all of the Xbufs and Xfiles fields in this struct set to 0. This
// will fail if THTTP_DEVMODE environment is not set to product
// mistakes in product deployments.
typedef struct thttp_request_tls {
thttp_request_t parent;
// 0 terminated list of 0 terminated certificate chain bufs (multiple chains)
char **crtbufs;
// 0 terminated list of certificate chain files
char **crtfiles;
// 0 terminated list of 0 terminated CRL chain bufs (multiple chains)
char **crlbufs;
// 0 terminated list of certificate chain files
char **crlfiles;
// null terminated list of ciphersuites from mbedtls defines. if NULL
// the reasonable defaults of mbedtls implementation will be picked.
// -- this guy is untyped to avoid dependencies to clients on mbedtls headers
// and
void *ciphersuites;
} thttp_request_tls_t;
typedef struct thttp_response {
thttp_method_t method;
thttp_proto_t proto;
thttp_proto_version_t proto_version;
char **headers;
char *body;
thttp_status_t code;
} thttp_response_t;
void thttp_set_log_func(void (*func)(int level, const char *fmt, va_list args));
void thttp_log(thttp_log_level_t level, const char *fmt, ...);
// full sync variant for http requests
thttp_response_t* thttp_request_do (thttp_request_t* req);
// save body to file instead of saving to buffer
// content-length will be set, but body will be null in response.
thttp_response_t* thttp_request_do_file (thttp_request_t *req, int fd);
thttp_response_t* thttp_request_do_file_with_cb (thttp_request_t *req,
int fd, void (*progress_cb)(ssize_t written, ssize_t chunksize, void *priv),
void *priv);
thttp_request_t* thttp_request_new_0 ();
thttp_request_tls_t* thttp_request_tls_new_0 ();
void thttp_request_free (thttp_request_t* ptr);
void thttp_response_free (thttp_response_t* ptr);
void thttp_add_headers(struct thttp_request *req, char **headers, int nr_headers);
thttp_status_t thttp_string_to_status (char *string);
const char* thttp_status_to_string (thttp_status_t status);
thttp_proto_t thttp_string_to_proto (char *string);
const char* thttp_proto_to_string (thttp_proto_t proto);
thttp_proto_version_t thttp_string_to_proto_version (char* string);
const char* thttp_proto_version_to_string (thttp_proto_version_t proto);
thttp_method_t
thttp_string_to_method (char *string);
const char*
thttp_method_to_string (thttp_method_t method);
#endif // THTTP_H