-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcurl_content_length_example.c
More file actions
267 lines (220 loc) · 8.48 KB
/
curl_content_length_example.c
File metadata and controls
267 lines (220 loc) · 8.48 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/http/connection.h>
#include <aws/http/request_response.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
#include <aws/io/host_resolver.h>
#include <aws/io/socket.h>
#include <aws/io/stream.h>
#include <aws/io/tls_channel_handler.h>
#include <aws/common/condition_variable.h>
#include <aws/common/mutex.h>
#include <aws/common/string.h>
#include <inttypes.h>
struct app_ctx {
struct aws_allocator *allocator;
struct aws_mutex mutex;
struct aws_condition_variable cv;
struct aws_http_connection *connection;
struct aws_http_stream *stream;
bool connection_completed;
bool stream_completed;
int error_code;
size_t bytes_to_send;
size_t bytes_sent;
};
static void s_on_write_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
(void)stream;
struct app_ctx *ctx = user_data;
if (error_code) {
fprintf(stderr, "Write failed with error: %s\n", aws_error_name(error_code));
return;
}
fprintf(stdout, "Write completed successfully\n");
}
static void s_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
(void)stream;
struct app_ctx *ctx = user_data;
aws_mutex_lock(&ctx->mutex);
ctx->stream_completed = true;
ctx->error_code = error_code;
aws_mutex_unlock(&ctx->mutex);
aws_condition_variable_notify_one(&ctx->cv);
if (error_code) {
fprintf(stderr, "Stream completed with error: %s\n", aws_error_name(error_code));
} else {
fprintf(stdout, "Stream completed successfully\n");
}
}
static void s_on_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
struct app_ctx *ctx = user_data;
aws_mutex_lock(&ctx->mutex);
if (error_code) {
fprintf(stderr, "Connection failed: %s\n", aws_error_name(error_code));
ctx->connection_completed = true;
ctx->error_code = error_code;
} else {
fprintf(stdout, "Connection established\n");
ctx->connection = connection;
}
aws_mutex_unlock(&ctx->mutex);
aws_condition_variable_notify_one(&ctx->cv);
}
static void s_on_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) {
(void)connection;
struct app_ctx *ctx = user_data;
aws_mutex_lock(&ctx->mutex);
ctx->connection_completed = true;
if (error_code) {
fprintf(stderr, "Connection shutdown with error: %s\n", aws_error_name(error_code));
}
aws_mutex_unlock(&ctx->mutex);
aws_condition_variable_notify_one(&ctx->cv);
}
static bool s_connection_ready(void *user_data) {
struct app_ctx *ctx = user_data;
return ctx->connection != NULL;
}
static bool s_stream_completed(void *user_data) {
struct app_ctx *ctx = user_data;
return ctx->stream_completed;
}
static bool s_connection_completed(void *user_data) {
struct app_ctx *ctx = user_data;
return ctx->connection_completed;
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
struct aws_allocator *allocator = aws_default_allocator();
aws_http_library_init(allocator);
struct app_ctx ctx = {
.allocator = allocator,
.bytes_to_send = 1024,
};
aws_mutex_init(&ctx.mutex);
aws_condition_variable_init(&ctx.cv);
/* Setup event loop */
struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
struct aws_host_resolver_default_options resolver_options = {
.el_group = el_group,
.max_entries = 8,
};
struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = el_group,
.host_resolver = resolver,
};
struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
/* Connect to httpbin.org */
struct aws_socket_options socket_options = {
.type = AWS_SOCKET_STREAM,
.domain = AWS_SOCKET_IPV4,
.connect_timeout_ms = 3000,
};
struct aws_http_client_connection_options conn_options = {
.self_size = sizeof(conn_options),
.socket_options = &socket_options,
.allocator = allocator,
.host_name = aws_byte_cursor_from_c_str("httpbin.org"),
.port = 80,
.bootstrap = bootstrap,
.on_setup = s_on_connection_setup,
.on_shutdown = s_on_connection_shutdown,
.user_data = &ctx,
};
aws_http_client_connect(&conn_options);
/* Wait for connection */
aws_mutex_lock(&ctx.mutex);
aws_condition_variable_wait_pred(&ctx.cv, &ctx.mutex, s_connection_ready, &ctx);
aws_mutex_unlock(&ctx.mutex);
if (!ctx.connection) {
fprintf(stderr, "Failed to establish connection\n");
goto cleanup;
}
/* Create request with Content-Length */
struct aws_http_message *request = aws_http_message_new_request(allocator);
if (!request) {
fprintf(stderr, "Failed to create request\n");
goto cleanup;
}
if (aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("POST"))) {
fprintf(stderr, "Failed to set request method\n");
aws_http_message_release(request);
goto cleanup;
}
if (aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/post"))) {
fprintf(stderr, "Failed to set request path\n");
aws_http_message_release(request);
goto cleanup;
}
struct aws_http_header headers[] = {
{.name = aws_byte_cursor_from_c_str("Host"), .value = aws_byte_cursor_from_c_str("httpbin.org")},
{.name = aws_byte_cursor_from_c_str("Content-Length"), .value = aws_byte_cursor_from_c_str("1024")},
{.name = aws_byte_cursor_from_c_str("Content-Type"), .value = aws_byte_cursor_from_c_str("text/plain")},
};
for (size_t i = 0; i < AWS_ARRAY_SIZE(headers); ++i) {
aws_http_message_add_header(request, headers[i]);
}
/* Make request with manual data writes */
/* Note: This minimal example demonstrates sending data but does not read the response */
struct aws_http_make_request_options options = {
.self_size = sizeof(options),
.request = request,
.use_manual_data_writes = true,
.on_complete = s_on_stream_complete,
.user_data = &ctx,
};
ctx.stream = aws_http_connection_make_request(ctx.connection, &options);
if (!ctx.stream) {
fprintf(stderr, "Failed to create stream\n");
aws_http_message_release(request);
goto cleanup;
}
aws_http_stream_activate(ctx.stream);
aws_http_message_release(request);
/* Write data in chunks */
uint8_t data[256];
memset(data, 'A', sizeof(data));
for (size_t i = 0; i < 4; ++i) {
struct aws_byte_cursor chunk = aws_byte_cursor_from_array(data, sizeof(data));
struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &chunk);
struct aws_http_stream_write_data_options write_options = {
.data = input_stream,
.end_stream = (i == 3),
.on_complete = s_on_write_complete,
.user_data = &ctx,
};
if (aws_http_stream_write_data(ctx.stream, &write_options)) {
fprintf(stderr, "Failed to write data: %s\n", aws_error_name(aws_last_error()));
aws_input_stream_release(input_stream);
break;
}
ctx.bytes_sent += sizeof(data);
fprintf(stdout, "Queued write %zu/%zu bytes\n", ctx.bytes_sent, ctx.bytes_to_send);
aws_input_stream_release(input_stream);
}
/* Wait for stream completion */
aws_mutex_lock(&ctx.mutex);
aws_condition_variable_wait_pred(&ctx.cv, &ctx.mutex, s_stream_completed, &ctx);
aws_mutex_unlock(&ctx.mutex);
aws_http_stream_release(ctx.stream);
cleanup:
if (ctx.connection) {
aws_http_connection_release(ctx.connection);
}
/* Wait for connection shutdown */
aws_mutex_lock(&ctx.mutex);
aws_condition_variable_wait_pred(&ctx.cv, &ctx.mutex, s_connection_completed, &ctx);
aws_mutex_unlock(&ctx.mutex);
aws_client_bootstrap_release(bootstrap);
aws_host_resolver_release(resolver);
aws_event_loop_group_release(el_group);
aws_condition_variable_clean_up(&ctx.cv);
aws_mutex_clean_up(&ctx.mutex);
aws_http_library_clean_up();
return ctx.error_code ? 1 : 0;
}