@@ -246,6 +246,7 @@ static hyper_io *create_io(conn_data *conn) {
246246typedef struct service_userdata_s {
247247 char host [128 ];
248248 char port [8 ];
249+ const hyper_executor * executor ;
249250} service_userdata ;
250251
251252static service_userdata * create_service_userdata () {
@@ -264,6 +265,26 @@ static int print_each_header(
264265 return HYPER_ITER_CONTINUE ;
265266}
266267
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+
267288static void server_callback (
268289 void * userdata , hyper_request * request , hyper_response_channel * channel
269290) {
@@ -301,11 +322,20 @@ static void server_callback(
301322 // Print out all the headers from the request
302323 hyper_headers * req_headers = hyper_request_headers (request );
303324 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
304334 hyper_request_free (request );
305335
306336 // Build a response
307337 hyper_response * response = hyper_response_new ();
308- hyper_response_set_status (response , 404 );
338+ hyper_response_set_status (response , 200 );
309339 hyper_headers * rsp_headers = hyper_response_headers (response );
310340 hyper_headers_set (
311341 rsp_headers ,
@@ -315,7 +345,17 @@ static void server_callback(
315345 8
316346 );
317347
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
319359 hyper_response_channel_send (channel , response );
320360}
321361
@@ -439,6 +479,7 @@ int main(int argc, char *argv[]) {
439479 listen_fd , (struct sockaddr * )& remote_addr_storage , & remote_addr_len
440480 )) >= 0 ) {
441481 service_userdata * userdata = create_service_userdata ();
482+ userdata -> executor = exec ;
442483 if (getnameinfo (
443484 remote_addr ,
444485 remote_addr_len ,
@@ -516,9 +557,9 @@ int main(int argc, char *argv[]) {
516557 }
517558 }
518559 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 ;
522563 } else {
523564 conn -> event_mask &= ~EPOLLOUT ;
524565 if (!update_conn_data_registrations (conn , false)) {
0 commit comments