aboutsummaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-06-22 15:16:11 +0200
committerChristian Grothoff <christian@grothoff.org>2018-06-22 15:16:11 +0200
commit832739556b765571f80520b91bdad5f14f6aa1b4 (patch)
tree79a7bf9a1c523466a3919a3c862426f17369a924 /src/examples
parent65a322cfe842d6028a0b27bc114b34ea178903eb (diff)
downloadlibmicrohttpd-832739556b765571f80520b91bdad5f14f6aa1b4.tar.gz
libmicrohttpd-832739556b765571f80520b91bdad5f14f6aa1b4.zip
some minor code cleaning issues
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/demo_https.c2
-rw-r--r--src/examples/querystring_example.c12
-rw-r--r--src/examples/suspend_resume_epoll.c68
3 files changed, 49 insertions, 33 deletions
diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c
index 88b01bbf..d5542eab 100644
--- a/src/examples/demo_https.c
+++ b/src/examples/demo_https.c
@@ -561,7 +561,7 @@ process_upload_data (void *cls,
561 uc->category, 561 uc->category,
562 filename); 562 filename);
563 for (i=strlen (fn)-1;i>=0;i--) 563 for (i=strlen (fn)-1;i>=0;i--)
564 if (! isprint ((int) fn[i])) 564 if (! isprint ((unsigned char) fn[i]))
565 fn[i] = '_'; 565 fn[i] = '_';
566 uc->fd = open (fn, 566 uc->fd = open (fn,
567 O_CREAT | O_EXCL 567 O_CREAT | O_EXCL
diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c
index 3d91bcea..db0d6f1a 100644
--- a/src/examples/querystring_example.c
+++ b/src/examples/querystring_example.c
@@ -77,16 +77,24 @@ int
77main (int argc, char *const *argv) 77main (int argc, char *const *argv)
78{ 78{
79 struct MHD_Daemon *d; 79 struct MHD_Daemon *d;
80 int port;
80 81
81 if (argc != 2) 82 if (argc != 2)
82 { 83 {
83 printf ("%s PORT\n", argv[0]); 84 printf ("%s PORT\n", argv[0]);
84 return 1; 85 return 1;
85 } 86 }
87 port = atoi (argv[1]);
88 if ( (port < 0) ||
89 (port > UINT16_MAX) )
90 {
91 printf ("%s PORT\n", argv[0]);
92 return 1;
93 }
86 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 94 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
87 atoi (argv[1]), 95 (uint16_t) port,
88 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 96 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
89 if (d == NULL) 97 if (NULL == d)
90 return 1; 98 return 1;
91 (void) getc (stdin); 99 (void) getc (stdin);
92 MHD_stop_daemon (d); 100 MHD_stop_daemon (d);
diff --git a/src/examples/suspend_resume_epoll.c b/src/examples/suspend_resume_epoll.c
index adff673c..4007cc94 100644
--- a/src/examples/suspend_resume_epoll.c
+++ b/src/examples/suspend_resume_epoll.c
@@ -54,69 +54,77 @@ ahc_echo (void *cls,
54 int ret; 54 int ret;
55 struct Request* req; 55 struct Request* req;
56 struct itimerspec ts; 56 struct itimerspec ts;
57 (void)url; /* Unused. Silence compiler warning. */
58 (void)version; /* Unused. Silence compiler warning. */
59 (void)upload_data; /* Unused. Silence compiler warning. */
60 (void)upload_data_size; /* Unused. Silence compiler warning. */
61 57
58 (void) url; /* Unused. Silence compiler warning. */
59 (void) version; /* Unused. Silence compiler warning. */
60 (void) upload_data; /* Unused. Silence compiler warning. */
61 (void) upload_data_size; /* Unused. Silence compiler warning. */
62 req = *ptr; 62 req = *ptr;
63 if (!req) 63 if (NULL == req)
64 { 64 {
65 65
66 req = malloc(sizeof(struct Request)); 66 req = malloc (sizeof(struct Request));
67 if (NULL == req)
68 return MHD_NO;
67 req->connection = connection; 69 req->connection = connection;
68 req->timerfd = 0; 70 req->timerfd = -1;
69 *ptr = req; 71 *ptr = req;
70 return MHD_YES; 72 return MHD_YES;
71 } 73 }
72 74
73 if (req->timerfd) 75 if (-1 != req->timerfd)
74 { 76 {
75 // send response (echo request url) 77 // send response (echo request url)
76 response = MHD_create_response_from_buffer (strlen (url), 78 response = MHD_create_response_from_buffer (strlen (url),
77 (void *) url, 79 (void *) url,
78 MHD_RESPMEM_MUST_COPY); 80 MHD_RESPMEM_MUST_COPY);
79 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 81 if (NULL == response)
82 return MHD_NO;
83 ret = MHD_queue_response (connection,
84 MHD_HTTP_OK,
85 response);
80 MHD_destroy_response (response); 86 MHD_destroy_response (response);
81 return ret; 87 return ret;
82 } 88 }
83 else 89 // create timer and suspend connection
84 { 90 req->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
85 // create timer and suspend connection 91 if (-1 == req->timerfd)
86 req->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
87 if (-1 == req->timerfd)
88 { 92 {
89 printf("timerfd_create: %s", strerror(errno)); 93 printf("timerfd_create: %s", strerror(errno));
90 return MHD_NO; 94 return MHD_NO;
91 } 95 }
92 evt.events = EPOLLIN; 96 evt.events = EPOLLIN;
93 evt.data.ptr = req; 97 evt.data.ptr = req;
94 if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, req->timerfd, &evt)) 98 if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, req->timerfd, &evt))
95 { 99 {
96 printf("epoll_ctl: %s", strerror(errno)); 100 printf("epoll_ctl: %s", strerror(errno));
97 return MHD_NO; 101 return MHD_NO;
98 } 102 }
99 ts.it_value.tv_sec = 1; 103 ts.it_value.tv_sec = 1;
100 ts.it_value.tv_nsec = 0; 104 ts.it_value.tv_nsec = 0;
101 ts.it_interval.tv_sec = 0; 105 ts.it_interval.tv_sec = 0;
102 ts.it_interval.tv_nsec = 0; 106 ts.it_interval.tv_nsec = 0;
103 if (-1 == timerfd_settime(req->timerfd, 0, &ts, NULL)) 107 if (-1 == timerfd_settime(req->timerfd, 0, &ts, NULL))
104 { 108 {
105 printf("timerfd_settime: %s", strerror(errno)); 109 printf("timerfd_settime: %s", strerror(errno));
106 return MHD_NO; 110 return MHD_NO;
107 } 111 }
108 MHD_suspend_connection(connection); 112 MHD_suspend_connection(connection);
109 return MHD_YES; 113 return MHD_YES;
110 }
111} 114}
112 115
113 116
114static int 117static void
115connection_done(struct MHD_Connection *connection, 118connection_done (void *cls,
116 void **con_cls, 119 struct MHD_Connection *connection,
117 enum MHD_RequestTerminationCode toe) 120 void **con_cls,
121 enum MHD_RequestTerminationCode toe)
118{ 122{
119 free(*con_cls); 123 struct Request *req = *con_cls;
124
125 if (-1 != req->timerfd)
126 close (req->timerfd);
127 free(req);
120} 128}
121 129
122 130