forked from HISONA/https_client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
241 lines (177 loc) · 6.03 KB
/
main.c
File metadata and controls
241 lines (177 loc) · 6.03 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <string.h>
#include "https.h"
int main(int argc, char *argv[])
{
char *url;
char data[1024], response[4096];
char err[100];
int i, ret, size;
int mode;
HTTP_INFO hi1, hi2;
// Init http session. verify: check the server CA cert.
http_init(&hi1, FALSE);
http_init(&hi2, TRUE);
// url = "https://wavegw.clova.ai/rswave/tts/1.6.5_23803-dcbee999d8a7452dde1e69d38aaceb31-1494844985383?tid=14dee084f5905f9c8f628cbd052ec529&hash=29581ae85d777e16445dbeda175c4fa4c344d437c79e5991ec5832557be3a805";
// url = argv[1];
// ret = http_get(&hi1, url, response, 4096);
// printf("len: %d, %s", ret, response);
/*
url = "https://localhost:8080/upload";
sprintf(data,
"--1234567890abcdef\r\n"
"Content-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n"
"Content-Type: text/plain\r\n\r\n"
"test message\r\n"
"--1234567890abcdef--\r\n\r\n"
);
ret = http_post(&hi1, url, data, response, sizeof(response));
printf("return code: %d \n", ret);
printf("return body: %s \n", response);
*/
url = "https://localhost:8080/upload";
if(http_open(&hi1, url) < 0)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
snprintf(hi1.request.method, 8, "POST");
hi1.request.close = FALSE;
hi1.request.chunked = TRUE;
snprintf(hi1.request.content_type, 256, "multipart/form-data; boundary=1234567890abcdef");
size = sprintf(data,
"--1234567890abcdef\r\n"
"Content-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n"
"Content-Type: text/plain\r\n\r\n"
"test message\r\n"
"--1234567890abcdef--\r\n"
);
hi1.request.content_length = size;
if(http_write_header(&hi1) < 0)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
if(http_write(&hi1, data, size) != size)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
// Write end-chunked
if(http_write_end(&hi1) < 0)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
http_read_init(&hi1);
while(1)
{
mode = http_parse(&hi1);
if((mode & HTTP_PARSE_WRITE) == HTTP_PARSE_WRITE)
{
printf("write: len: %ld, %s \n", hi1.body_len, hi1.body);
}
if((mode & HTTP_PARSE_READ) == HTTP_PARSE_READ)
{
ret = http_read(&hi1);
if (ret < 0)
{
mbedtls_strerror(ret, err, 100);
printf("socket error: %s(%d)", err, ret);
break;
}
else if (ret == 0)
{
break;
}
}
else if((mode & HTTP_PARSE_CHUNK) == HTTP_PARSE_CHUNK)
{
printf("return: HTTP_PARSE_CHUNK: chunk_size: %ld \n", hi1.chunk_size);
}
else if((mode & HTTP_PARSE_END) == HTTP_PARSE_END)
{
printf("return: HTTP_PARSE_END: content_length: %ld \n", hi1.response.content_length);
break;
}
else if((mode & HTTP_PARSE_ERROR) == HTTP_PARSE_ERROR)
{
snprintf(response, 256, "http parse error.");
break;
}
}
if(hi1.response.close == TRUE)
{
http_close(&hi1);
}
/*
// Test a http get method.
url = "http://httpbin.org/get?message=https_client";
ret = http_get(&hi1, url, response, sizeof(response));
printf("return code: %d \n", hi1.response.status);
printf("return body: len: %d, %s \n", ret, response);
// Test a http post method.
url = "http://httpbin.org/post";
sprintf(data, "{\"message\":\"Hello, https_client!\"}");
ret = http_post(&hi1, url, data, response, sizeof(response));
printf("return code: %d \n", hi1.response.status);
printf("return body: len: %d, %s \n", ret, response);
// Test a https get method.
url = "https://httpbin.org/get?message=https_client";
ret = http_get(&hi2, url, response, sizeof(response));
printf("return code: %d \n", hi1.response.status);
printf("return body: len: %d, %s \n", ret, response);
// Test a https post method.
url = "https://httpbin.org/post";
sprintf(data, "{\"message\":\"Hello, https_client!\"}");
ret = http_post(&hi2, url, data, response, sizeof(response));
printf("return code: %d \n", hi1.response.status);
printf("return body: len: %d, %s \n", ret, response);
// Test a https post with the chunked-encoding data.
url = "https://httpbin.org/post";
if(http_open_chunked(&hi2, url) == 0)
{
size = sprintf(data, "[{\"message\":\"Hello, https_client %d\"},", 0);
if(http_write_chunked(&hi2, data, size) != size)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
for(i=1; i<4; i++)
{
size = sprintf(data, "{\"message\":\"Hello, https_client %d\"},", i);
if(http_write_chunked(&hi2, data, size) != size)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
}
size = sprintf(data, "{\"message\":\"Hello, https_client %d\"}]", i);
if(http_write_chunked(&hi2, data, strlen(data)) != size)
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
goto error;
}
ret = http_read_chunked(&hi2, response, sizeof(response));
printf("return code: %d \n", ret);
printf("return body: %s \n", response);
}
else
{
http_strerror(data, 1024);
printf("socket error: %s \n", data);
}
error:
*/
error:
http_close(&hi1);
http_close(&hi2);
return 0;
}