http server: improvements
[vpp.git] / src / vnet / session-apps / http_server.c
1 /*
2 * Copyright (c) 2015-2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <vnet/vnet.h>
17 #include <vnet/session/application.h>
18 #include <vnet/session/application_interface.h>
19
20 typedef enum
21 {
22   EVENT_WAKEUP = 1,
23 } http_process_event_t;
24
25 typedef struct
26 {
27   u32 hs_index;
28   u32 thread_index;
29   u64 node_index;
30 } http_server_args;
31
32 typedef enum
33 {
34   HTTP_STATE_CLOSED,
35   HTTP_STATE_ESTABLISHED,
36   HTTP_STATE_OK_SENT,
37 } http_session_state_t;
38 typedef struct
39 {
40   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
41 #define _(type, name) type name;
42   foreach_app_session_field
43 #undef _
44   u32 thread_index;
45   u8 *rx_buf;
46   u32 vpp_session_index;
47   u64 vpp_session_handle;
48 } http_session_t;
49
50 typedef struct
51 {
52   http_session_t **sessions;
53   clib_rwlock_t sessions_lock;
54   u32 **session_to_http_session;
55
56   svm_msg_q_t **vpp_queue;
57
58   uword *handler_by_get_request;
59
60   u32 *free_http_cli_process_node_indices;
61
62   /* Sever's event queue */
63   svm_queue_t *vl_input_queue;
64
65   /* API client handle */
66   u32 my_client_index;
67
68   u32 app_index;
69
70   /* process node index for evnt scheduling */
71   u32 node_index;
72
73   u32 prealloc_fifos;
74   u32 private_segment_size;
75   u32 fifo_size;
76   u8 *uri;
77   u32 is_static;
78   vlib_main_t *vlib_main;
79 } http_server_main_t;
80
81 http_server_main_t http_server_main;
82
83 static void
84 http_server_sessions_reader_lock (void)
85 {
86   clib_rwlock_reader_lock (&http_server_main.sessions_lock);
87 }
88
89 static void
90 http_server_sessions_reader_unlock (void)
91 {
92   clib_rwlock_reader_unlock (&http_server_main.sessions_lock);
93 }
94
95 static void
96 http_server_sessions_writer_lock (void)
97 {
98   clib_rwlock_writer_lock (&http_server_main.sessions_lock);
99 }
100
101 static void
102 http_server_sessions_writer_unlock (void)
103 {
104   clib_rwlock_writer_unlock (&http_server_main.sessions_lock);
105 }
106
107 static void
108 http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index)
109 {
110   http_server_main_t *hsm = &http_server_main;
111   vec_validate (hsm->session_to_http_session[thread_index], s_index);
112   hsm->session_to_http_session[thread_index][s_index] = hs_index;
113 }
114
115 static void
116 http_server_session_lookup_del (u32 thread_index, u32 s_index)
117 {
118   http_server_main_t *hsm = &http_server_main;
119   hsm->session_to_http_session[thread_index][s_index] = ~0;
120 }
121
122 static http_session_t *
123 http_server_session_lookup (u32 thread_index, u32 s_index)
124 {
125   http_server_main_t *hsm = &http_server_main;
126   u32 hs_index;
127
128   if (s_index < vec_len (hsm->session_to_http_session[thread_index]))
129     {
130       hs_index = hsm->session_to_http_session[thread_index][s_index];
131       if (hs_index < vec_len (hsm->sessions[thread_index]))
132         return &hsm->sessions[thread_index][hs_index];
133     }
134   return 0;
135 }
136
137 static http_session_t *
138 http_server_session_alloc (u32 thread_index)
139 {
140   http_server_main_t *hsm = &http_server_main;
141   http_session_t *hs;
142   pool_get (hsm->sessions[thread_index], hs);
143   memset (hs, 0, sizeof (*hs));
144   hs->session_index = hs - hsm->sessions[thread_index];
145   hs->thread_index = thread_index;
146   return hs;
147 }
148
149 static http_session_t *
150 http_server_session_get (u32 thread_index, u32 hs_index)
151 {
152   http_server_main_t *hsm = &http_server_main;
153   return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
154 }
155
156 static void
157 http_server_session_free (http_session_t * hs)
158 {
159   http_server_main_t *hsm = &http_server_main;
160   pool_put (hsm->sessions[hs->thread_index], hs);
161   if (CLIB_DEBUG)
162     memset (hs, 0xfa, sizeof (*hs));
163 }
164
165 static void
166 http_server_session_cleanup (http_session_t * hs)
167 {
168   if (!hs)
169     return;
170   http_server_session_lookup_del (hs->thread_index, hs->vpp_session_index);
171   vec_free (hs->rx_buf);
172   http_server_session_free (hs);
173 }
174
175 static void
176 http_process_free (http_server_args * args)
177 {
178   vlib_node_runtime_t *rt;
179   vlib_main_t *vm = &vlib_global_main;
180   http_server_main_t *hsm = &http_server_main;
181   vlib_node_t *n;
182   u32 node_index;
183   http_server_args **save_args;
184
185   node_index = args->node_index;
186   ASSERT (node_index != 0);
187
188   n = vlib_get_node (vm, node_index);
189   rt = vlib_node_get_runtime (vm, n->index);
190   save_args = vlib_node_get_runtime_data (vm, n->index);
191
192   /* Reset process session pointer */
193   clib_mem_free (*save_args);
194   *save_args = 0;
195
196   /* Turn off the process node */
197   vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
198
199   /* add node index to the freelist */
200   vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
201 }
202
203 /* *INDENT-OFF* */
204 static const char *http_ok =
205     "HTTP/1.1 200 OK\r\n";
206
207 static const char *http_response =
208     "Content-Type: text/html\r\n"
209     "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
210     "Connection: close \r\n"
211     "Pragma: no-cache\r\n"
212     "Content-Length: %d\r\n\r\n%s";
213
214 static const char *http_error_template =
215     "HTTP/1.1 %s\r\n"
216     "Content-Type: text/html\r\n"
217     "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
218     "Connection: close\r\n"
219     "Pragma: no-cache\r\n"
220     "Content-Length: 0\r\n\r\n";
221
222 /* Header, including incantation to suppress favicon.ico requests */
223 static const char *html_header_template =
224     "<html><head><title>%v</title></head>"
225     "<link rel=\"icon\" href=\"data:,\">"
226     "<body><pre>";
227
228 static const char *html_footer =
229     "</pre></body></html>\r\n";
230
231 static const char *html_header_static =
232     "<html><head><title>static reply</title></head>"
233     "<link rel=\"icon\" href=\"data:,\">"
234     "<body><pre>hello</pre></body></html>\r\n";
235 /* *INDENT-ON* */
236
237 static u8 *static_http;
238 static u8 *static_ok;
239
240 static void
241 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
242 {
243   u8 **output_vecp = (u8 **) arg;
244   u8 *output_vec;
245   u32 offset;
246
247   output_vec = *output_vecp;
248
249   offset = vec_len (output_vec);
250   vec_validate (output_vec, offset + buffer_bytes - 1);
251   clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes);
252
253   *output_vecp = output_vec;
254 }
255
256 void
257 send_data (http_session_t * hs, u8 * data)
258 {
259   http_server_main_t *hsm = &http_server_main;
260   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
261   vlib_main_t *vm = vlib_get_main ();
262   f64 last_sent_timer = vlib_time_now (vm);
263   u32 offset, bytes_to_send;
264   f64 delay = 10e-3;
265
266   bytes_to_send = vec_len (data);
267   offset = 0;
268
269   while (bytes_to_send > 0)
270     {
271       int actual_transfer;
272
273       actual_transfer = svm_fifo_enqueue_nowait
274         (hs->tx_fifo, bytes_to_send, data + offset);
275
276       /* Made any progress? */
277       if (actual_transfer <= 0)
278         {
279           vlib_process_suspend (vm, delay);
280           /* 10s deadman timer */
281           if (vlib_time_now (vm) > last_sent_timer + 10.0)
282             {
283               a->handle = hs->vpp_session_handle;
284               a->app_index = hsm->app_index;
285               vnet_disconnect_session (a);
286               break;
287             }
288           /* Exponential backoff, within reason */
289           if (delay < 1.0)
290             delay = delay * 2.0;
291         }
292       else
293         {
294           last_sent_timer = vlib_time_now (vm);
295           offset += actual_transfer;
296           bytes_to_send -= actual_transfer;
297
298           if (svm_fifo_set_event (hs->tx_fifo))
299             session_send_io_evt_to_thread (hs->tx_fifo,
300                                            SESSION_IO_EVT_TX_FLUSH);
301           delay = 10e-3;
302         }
303     }
304 }
305
306 static void
307 send_error (http_session_t * hs, char *str)
308 {
309   u8 *data;
310
311   data = format (0, http_error_template, str);
312   send_data (hs, data);
313   vec_free (data);
314 }
315
316 static uword
317 http_cli_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
318                   vlib_frame_t * f)
319 {
320   u8 *request = 0, *reply = 0, *http = 0, *html = 0;
321   http_server_main_t *hsm = &http_server_main;
322   http_server_args **save_args;
323   http_server_args *args;
324   unformat_input_t input;
325   http_session_t *hs;
326   int i;
327
328   save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
329   args = *save_args;
330
331   http_server_sessions_reader_lock ();
332
333   hs = http_server_session_get (args->thread_index, args->hs_index);
334   ASSERT (hs);
335
336   request = hs->rx_buf;
337   if (vec_len (request) < 7)
338     {
339       send_error (hs, "400 Bad Request");
340       goto out;
341     }
342
343   for (i = 0; i < vec_len (request) - 4; i++)
344     {
345       if (request[i] == 'G' &&
346           request[i + 1] == 'E' &&
347           request[i + 2] == 'T' && request[i + 3] == ' ')
348         goto found;
349     }
350 bad_request:
351   send_error (hs, "400 Bad Request");
352   goto out;
353
354 found:
355   /* Lose "GET " */
356   vec_delete (request, i + 5, 0);
357
358   /* Replace slashes with spaces, stop at the end of the path */
359   i = 0;
360   while (1)
361     {
362       if (request[i] == '/')
363         request[i] = ' ';
364       else if (request[i] == ' ')
365         {
366           /* vlib_cli_input is vector-based, no need for a NULL */
367           _vec_len (request) = i;
368           break;
369         }
370       i++;
371       /* Should never happen */
372       if (i == vec_len (request))
373         goto bad_request;
374     }
375
376   /* Generate the html header */
377   html = format (0, html_header_template, request /* title */ );
378
379   /* Run the command */
380   unformat_init_vector (&input, vec_dup (request));
381   vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
382   unformat_free (&input);
383   request = 0;
384
385   /* Generate the html page */
386   html = format (html, "%v", reply);
387   html = format (html, html_footer);
388   /* And the http reply */
389   http = format (0, http_ok, vec_len (http_ok));
390   http = format (http, http_response, vec_len (html), html);
391
392   /* Send it */
393   send_data (hs, http);
394
395 out:
396   /* Cleanup */
397   http_server_sessions_reader_unlock ();
398   vec_free (reply);
399   vec_free (html);
400   vec_free (http);
401
402   http_process_free (args);
403   return (0);
404 }
405
406 static void
407 alloc_http_process (http_server_args * args)
408 {
409   char *name;
410   vlib_node_t *n;
411   http_server_main_t *hsm = &http_server_main;
412   vlib_main_t *vm = hsm->vlib_main;
413   uword l = vec_len (hsm->free_http_cli_process_node_indices);
414   http_server_args **save_args;
415
416   if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
417     {
418       n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
419       vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
420       _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
421     }
422   else
423     {
424       static vlib_node_registration_t r = {
425         .function = http_cli_process,
426         .type = VLIB_NODE_TYPE_PROCESS,
427         .process_log2_n_stack_bytes = 16,
428         .runtime_data_bytes = sizeof (void *),
429       };
430
431       name = (char *) format (0, "http-cli-%d", l);
432       r.name = name;
433       vlib_register_node (vm, &r);
434       vec_free (name);
435
436       n = vlib_get_node (vm, r.index);
437     }
438
439   /* Save the node index in the args. It won't be zero. */
440   args->node_index = n->index;
441
442   /* Save the args (pointer) in the node runtime */
443   save_args = vlib_node_get_runtime_data (vm, n->index);
444   *save_args = args;
445
446   vlib_start_process (vm, n->runtime_index);
447 }
448
449 static void
450 alloc_http_process_callback (void *cb_args)
451 {
452   alloc_http_process ((http_server_args *) cb_args);
453 }
454
455 static int
456 session_rx_request (http_session_t * hs)
457 {
458   u32 max_dequeue, cursize;
459   int n_read;
460
461   cursize = vec_len (hs->rx_buf);
462   max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo);
463   if (PREDICT_FALSE (max_dequeue == 0))
464     return -1;
465
466   vec_validate (hs->rx_buf, cursize + max_dequeue - 1);
467   n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize,
468                                 max_dequeue, 0, 0 /* peek */ );
469   ASSERT (n_read == max_dequeue);
470   if (svm_fifo_is_empty (hs->rx_fifo))
471     svm_fifo_unset_event (hs->rx_fifo);
472
473   _vec_len (hs->rx_buf) = cursize + n_read;
474   return 0;
475 }
476
477 static int
478 http_server_rx_callback (stream_session_t * s)
479 {
480   http_server_args *args;
481   http_session_t *hs;
482   int rv;
483
484   http_server_sessions_reader_lock ();
485
486   hs = http_server_session_lookup (s->thread_index, s->session_index);
487   if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED)
488     return -1;
489
490   rv = session_rx_request (hs);
491   if (rv)
492     return rv;
493
494   /* send the command to a new/recycled vlib process */
495   args = clib_mem_alloc (sizeof (*args));
496   args->hs_index = hs->session_index;
497   args->thread_index = hs->thread_index;
498
499   http_server_sessions_reader_unlock ();
500
501   /* Send an RPC request via the thread-0 input node */
502   if (vlib_get_thread_index () != 0)
503     session_send_rpc_evt_to_thread (0, alloc_http_process_callback, args);
504   else
505     alloc_http_process (args);
506   return 0;
507 }
508
509 static int
510 http_server_rx_callback_static (stream_session_t * s)
511 {
512   http_server_main_t *hsm = &http_server_main;
513   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
514   http_session_t *hs;
515   u32 request_len;
516   u8 *request = 0;
517   int i, rv;
518
519   hs = http_server_session_lookup (s->thread_index, s->session_index);
520   if (!hs || hs->session_state == HTTP_STATE_CLOSED)
521     return 0;
522
523   /* ok 200 was sent */
524   if (hs->session_state == HTTP_STATE_OK_SENT)
525     goto send_data;
526
527   rv = session_rx_request (hs);
528   if (rv)
529     goto wait_for_data;
530
531   request = hs->rx_buf;
532   request_len = vec_len (request);
533   if (vec_len (request) < 7)
534     {
535       send_error (hs, "400 Bad Request");
536       goto close_session;
537     }
538
539   for (i = 0; i < request_len - 4; i++)
540     {
541       if (request[i] == 'G' &&
542           request[i + 1] == 'E' &&
543           request[i + 2] == 'T' && request[i + 3] == ' ')
544         goto find_end;
545     }
546   send_error (hs, "400 Bad Request");
547   goto close_session;
548
549 find_end:
550
551   /* check for the end sequence: /r/n/r/n */
552   if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa
553       || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd)
554     goto wait_for_data;
555
556   /* send 200 OK first */
557   send_data (hs, static_ok);
558   hs->session_state = HTTP_STATE_OK_SENT;
559   goto postpone;
560
561 send_data:
562   send_data (hs, static_http);
563   http_server_session_cleanup (hs);
564
565 close_session:
566   a->handle = session_handle (s);
567   a->app_index = hsm->app_index;
568   vnet_disconnect_session (a);
569   return 0;
570
571 postpone:
572   svm_fifo_set_event (hs->rx_fifo);
573   session_send_io_evt_to_thread (hs->rx_fifo, FIFO_EVENT_BUILTIN_RX);
574   return 0;
575
576 wait_for_data:
577   return 0;
578 }
579
580 static int
581 http_server_session_accept_callback (stream_session_t * s)
582 {
583   http_server_main_t *hsm = &http_server_main;
584   http_session_t *hs;
585
586   hsm->vpp_queue[s->thread_index] =
587     session_manager_get_vpp_event_queue (s->thread_index);
588
589   if (!hsm->is_static)
590     http_server_sessions_writer_lock ();
591
592   hs = http_server_session_alloc (s->thread_index);
593   http_server_session_lookup_add (s->thread_index, s->session_index,
594                                   hs->session_index);
595   hs->rx_fifo = s->server_rx_fifo;
596   hs->tx_fifo = s->server_tx_fifo;
597   hs->vpp_session_index = s->session_index;
598   hs->vpp_session_handle = session_handle (s);
599   hs->session_state = HTTP_STATE_ESTABLISHED;
600
601   if (!hsm->is_static)
602     http_server_sessions_writer_unlock ();
603
604   s->session_state = SESSION_STATE_READY;
605   return 0;
606 }
607
608 static void
609 http_server_session_disconnect_callback (stream_session_t * s)
610 {
611   http_server_main_t *hsm = &http_server_main;
612   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
613   http_session_t *hs;
614
615   if (!hsm->is_static)
616     http_server_sessions_writer_lock ();
617
618   hs = http_server_session_lookup (s->thread_index, s->session_index);
619   http_server_session_cleanup (hs);
620
621   if (!hsm->is_static)
622     http_server_sessions_writer_unlock ();
623
624   a->handle = session_handle (s);
625   a->app_index = hsm->app_index;
626   vnet_disconnect_session (a);
627 }
628
629 static void
630 http_server_session_reset_callback (stream_session_t * s)
631 {
632   http_server_main_t *hsm = &http_server_main;
633   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
634   http_session_t *hs;
635
636   if (!hsm->is_static)
637     http_server_sessions_writer_lock ();
638
639   hs = http_server_session_lookup (s->thread_index, s->session_index);
640   http_server_session_cleanup (hs);
641
642   if (!hsm->is_static)
643     http_server_sessions_writer_unlock ();
644
645   a->handle = session_handle (s);
646   a->app_index = hsm->app_index;
647   vnet_disconnect_session (a);
648 }
649
650 static int
651 http_server_session_connected_callback (u32 app_index, u32 api_context,
652                                         stream_session_t * s, u8 is_fail)
653 {
654   clib_warning ("called...");
655   return -1;
656 }
657
658 static int
659 http_server_add_segment_callback (u32 client_index, u64 segment_handle)
660 {
661   clib_warning ("called...");
662   return -1;
663 }
664
665 static session_cb_vft_t http_server_session_cb_vft = {
666   .session_accept_callback = http_server_session_accept_callback,
667   .session_disconnect_callback = http_server_session_disconnect_callback,
668   .session_connected_callback = http_server_session_connected_callback,
669   .add_segment_callback = http_server_add_segment_callback,
670   .builtin_app_rx_callback = http_server_rx_callback,
671   .session_reset_callback = http_server_session_reset_callback
672 };
673
674 static int
675 http_server_attach ()
676 {
677   vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
678   vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
679   http_server_main_t *hsm = &http_server_main;
680   u64 options[APP_OPTIONS_N_OPTIONS];
681   vnet_app_attach_args_t _a, *a = &_a;
682   u32 segment_size = 128 << 20;
683
684   clib_memset (a, 0, sizeof (*a));
685   clib_memset (options, 0, sizeof (options));
686
687   if (hsm->private_segment_size)
688     segment_size = hsm->private_segment_size;
689
690   a->api_client_index = ~0;
691   a->name = format (0, "test_http_server");
692   a->session_cb_vft = &http_server_session_cb_vft;
693   a->options = options;
694   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
695   a->options[APP_OPTIONS_RX_FIFO_SIZE] =
696     hsm->fifo_size ? hsm->fifo_size : 8 << 10;
697   a->options[APP_OPTIONS_TX_FIFO_SIZE] =
698     hsm->fifo_size ? hsm->fifo_size : 32 << 10;
699   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
700   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
701
702   if (vnet_application_attach (a))
703     {
704       vec_free (a->name);
705       clib_warning ("failed to attach server");
706       return -1;
707     }
708   vec_free (a->name);
709   hsm->app_index = a->app_index;
710
711   clib_memset (a_cert, 0, sizeof (*a_cert));
712   a_cert->app_index = a->app_index;
713   vec_validate (a_cert->cert, test_srv_crt_rsa_len);
714   clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
715   vnet_app_add_tls_cert (a_cert);
716
717   clib_memset (a_key, 0, sizeof (*a_key));
718   a_key->app_index = a->app_index;
719   vec_validate (a_key->key, test_srv_key_rsa_len);
720   clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
721   vnet_app_add_tls_key (a_key);
722
723   return 0;
724 }
725
726 static int
727 http_server_listen ()
728 {
729   http_server_main_t *hsm = &http_server_main;
730   vnet_bind_args_t _a, *a = &_a;
731   clib_memset (a, 0, sizeof (*a));
732   a->app_index = hsm->app_index;
733   a->uri = "tcp://0.0.0.0/80";
734   if (hsm->uri)
735     a->uri = (char *) hsm->uri;
736   return vnet_bind_uri (a);
737 }
738
739 static int
740 http_server_create (vlib_main_t * vm)
741 {
742   vlib_thread_main_t *vtm = vlib_get_thread_main ();
743   http_server_main_t *hsm = &http_server_main;
744   u32 num_threads;
745
746   num_threads = 1 /* main thread */  + vtm->n_threads;
747   vec_validate (hsm->vpp_queue, num_threads - 1);
748   vec_validate (hsm->sessions, num_threads - 1);
749   vec_validate (hsm->session_to_http_session, num_threads - 1);
750
751   clib_rwlock_init (&hsm->sessions_lock);
752
753   if (http_server_attach ())
754     {
755       clib_warning ("failed to attach server");
756       return -1;
757     }
758   if (http_server_listen ())
759     {
760       clib_warning ("failed to start listening");
761       return -1;
762     }
763   return 0;
764 }
765
766 static clib_error_t *
767 http_server_create_command_fn (vlib_main_t * vm,
768                                unformat_input_t * input,
769                                vlib_cli_command_t * cmd)
770 {
771   http_server_main_t *hsm = &http_server_main;
772   u64 seg_size;
773   u8 *html;
774   int rv;
775
776   hsm->prealloc_fifos = 0;
777   hsm->private_segment_size = 0;
778   hsm->fifo_size = 0;
779   hsm->is_static = 0;
780   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
781     {
782       if (unformat (input, "static"))
783         hsm->is_static = 1;
784       else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos))
785         ;
786       else if (unformat (input, "private-segment-size %U",
787                          unformat_memory_size, &seg_size))
788         {
789           if (seg_size >= 0x100000000ULL)
790             {
791               vlib_cli_output (vm, "private segment size %llu, too large",
792                                seg_size);
793               return 0;
794             }
795           hsm->private_segment_size = seg_size;
796         }
797       else if (unformat (input, "fifo-size %d", &hsm->fifo_size))
798         hsm->fifo_size <<= 10;
799       else if (unformat (input, "uri %s", &hsm->uri))
800         ;
801       else
802         return clib_error_return (0, "unknown input `%U'",
803                                   format_unformat_error, input);
804     }
805   if (hsm->my_client_index != (u32) ~ 0)
806     return clib_error_return (0, "test http server is already running");
807
808   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
809
810   if (hsm->is_static)
811     {
812       http_server_session_cb_vft.builtin_app_rx_callback =
813         http_server_rx_callback_static;
814       html = format (0, html_header_static);
815       static_http = format (0, http_response, vec_len (html), html);
816       static_ok = format (0, http_ok);
817     }
818   rv = http_server_create (vm);
819   switch (rv)
820     {
821     case 0:
822       break;
823     default:
824       return clib_error_return (0, "server_create returned %d", rv);
825     }
826   return 0;
827 }
828
829 /* *INDENT-OFF* */
830 VLIB_CLI_COMMAND (http_server_create_command, static) =
831 {
832   .path = "test http server",
833   .short_help = "test http server",
834   .function = http_server_create_command_fn,
835 };
836 /* *INDENT-ON* */
837
838 static clib_error_t *
839 http_server_main_init (vlib_main_t * vm)
840 {
841   http_server_main_t *hsm = &http_server_main;
842
843   hsm->my_client_index = ~0;
844   hsm->vlib_main = vm;
845   return 0;
846 }
847
848 VLIB_INIT_FUNCTION (http_server_main_init);
849
850 /*
851 * fd.io coding-style-patch-verification: ON
852 *
853 * Local Variables:
854 * eval: (c-set-style "gnu")
855 * End:
856 */