@@ -246,6 +246,7 @@ static hyper_io *create_io(conn_data *conn) {
246
246
typedef struct service_userdata_s {
247
247
char host [128 ];
248
248
char port [8 ];
249
+ const hyper_executor * executor ;
249
250
} service_userdata ;
250
251
251
252
static service_userdata * create_service_userdata () {
@@ -264,6 +265,26 @@ static int print_each_header(
264
265
return HYPER_ITER_CONTINUE ;
265
266
}
266
267
268
+ static int print_body_chunk (void * userdata , const hyper_buf * chunk ) {
269
+ const uint8_t * buf = hyper_buf_bytes (chunk );
270
+ size_t len = hyper_buf_len (chunk );
271
+ write (1 , buf , len );
272
+ return HYPER_ITER_CONTINUE ;
273
+ }
274
+
275
+ static int send_each_body_chunk (void * userdata , hyper_context * ctx , hyper_buf * * chunk ) {
276
+ int * chunk_count = (int * )userdata ;
277
+ if (* chunk_count > 0 ) {
278
+ unsigned char data [4096 ];
279
+ memset (data , '0' + (* chunk_count % 10 ), sizeof (data ));
280
+ * chunk = hyper_buf_copy (data , sizeof (data ));
281
+ (* chunk_count )-- ;
282
+ } else {
283
+ * chunk = NULL ;
284
+ }
285
+ return HYPER_POLL_READY ;
286
+ }
287
+
267
288
static void server_callback (
268
289
void * userdata , hyper_request * request , hyper_response_channel * channel
269
290
) {
@@ -301,11 +322,20 @@ static void server_callback(
301
322
// Print out all the headers from the request
302
323
hyper_headers * req_headers = hyper_request_headers (request );
303
324
hyper_headers_foreach (req_headers , print_each_header , NULL );
325
+
326
+ if (!strcmp ((char * )method , "POST" ) || !strcmp ((char * )method , "PUT" )) {
327
+ // ...consume the request body
328
+ hyper_body * body = hyper_request_body (request );
329
+ hyper_task * task = hyper_body_foreach (body , print_body_chunk , NULL , NULL );
330
+ hyper_executor_push (service_data -> executor , task );
331
+ }
332
+
333
+ // Tidy up
304
334
hyper_request_free (request );
305
335
306
336
// Build a response
307
337
hyper_response * response = hyper_response_new ();
308
- hyper_response_set_status (response , 404 );
338
+ hyper_response_set_status (response , 200 );
309
339
hyper_headers * rsp_headers = hyper_response_headers (response );
310
340
hyper_headers_set (
311
341
rsp_headers ,
@@ -315,7 +345,17 @@ static void server_callback(
315
345
8
316
346
);
317
347
318
- // And send the response, completing the transaction
348
+ if (!strcmp ((char * )method , "GET" )) {
349
+ // ...add a body
350
+ hyper_body * body = hyper_body_new ();
351
+ hyper_body_set_data_func (body , send_each_body_chunk );
352
+ int * chunk_count = (int * )malloc (sizeof (int ));
353
+ * chunk_count = 1000 ;
354
+ hyper_body_set_userdata (body , (void * )chunk_count , free );
355
+ hyper_response_set_body (response , body );
356
+ }
357
+
358
+ // ...and send the response, completing the transaction
319
359
hyper_response_channel_send (channel , response );
320
360
}
321
361
@@ -439,6 +479,7 @@ int main(int argc, char *argv[]) {
439
479
listen_fd , (struct sockaddr * )& remote_addr_storage , & remote_addr_len
440
480
)) >= 0 ) {
441
481
service_userdata * userdata = create_service_userdata ();
482
+ userdata -> executor = exec ;
442
483
if (getnameinfo (
443
484
remote_addr ,
444
485
remote_addr_len ,
@@ -516,9 +557,9 @@ int main(int argc, char *argv[]) {
516
557
}
517
558
}
518
559
if (events [n ].events & EPOLLOUT ) {
519
- if (conn -> read_waker ) {
520
- hyper_waker_wake (conn -> read_waker );
521
- conn -> read_waker = NULL ;
560
+ if (conn -> write_waker ) {
561
+ hyper_waker_wake (conn -> write_waker );
562
+ conn -> write_waker = NULL ;
522
563
} else {
523
564
conn -> event_mask &= ~EPOLLOUT ;
524
565
if (!update_conn_data_registrations (conn , false)) {
0 commit comments