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
187 lines (133 loc) · 4.5 KB
/
main.c
File metadata and controls
187 lines (133 loc) · 4.5 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
#include <stdio.h>
#include <string.h>
#include "https.h"
int main(int argc, char *argv[])
{
char *url;
char data[1024], response[4096];
int i, ret, size;
HTTP_INFO hi1, hi2;
// Init http session. verify: check the server CA cert.
http_init(&hi1, FALSE);
http_init(&hi2, TRUE);
/*
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 = FALSE;
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;
}
ret = http_read_chunked(&hi1, response, sizeof(response));
printf("return code: %d \n", ret);
printf("return body: %s \n", response);
/*
// 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", ret);
printf("return body: %s \n", 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", ret);
printf("return body: %s \n", 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", ret);
printf("return body: %s \n", 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", ret);
printf("return body: %s \n", 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;
}