Add to git.
[rws.git] / examples / show_params.c
1 /* More complex example shared object script showing parameter parsing.
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: show_params.c,v 1.4 2002/12/01 16:16:04 rich Exp $
19  */
20
21 #include <pool.h>
22 #include <vector.h>
23 #include <pthr_cgi.h>
24
25 #include "rws_request.h"
26
27 int
28 handle_request (rws_request rq)
29 {
30   pool pool = pth_get_pool (current_pth);
31   http_request http_request = rws_request_http_request (rq);
32   io_handle io = rws_request_io (rq);
33
34   cgi cgi;
35   int close, i;
36   http_response http_response;
37   vector headers, params;
38   struct http_header header;
39   const char *name, *value;
40
41   /* Parse CGI parameters. */
42   cgi = new_cgi (pool, http_request, io);
43
44   /* Begin response. */
45   http_response = new_http_response (pool, http_request, io,
46                                      200, "OK");
47   http_response_send_headers (http_response,
48                               /* Content type. */
49                               "Content-Type", "text/plain",
50                               /* End of headers. */
51                               NULL);
52   close = http_response_end_headers (http_response);
53
54   if (http_request_is_HEAD (http_request)) return close;
55
56   io_fprintf (io, "This is the show_params shared object script.\r\n\r\n");
57   io_fprintf (io, "Your browser sent the following headers:\r\n\r\n");
58
59   headers = http_request_get_headers (http_request);
60   for (i = 0; i < vector_size (headers); ++i)
61     {
62       vector_get (headers, i, header);
63       io_fprintf (io, "\t%s: %s\r\n", header.key, header.value);
64     }
65
66   io_fprintf (io, "----- end of headers -----\r\n");
67
68   io_fprintf (io, "The URL was: %s\r\n",
69               http_request_get_url (http_request));
70   io_fprintf (io, "The path component was: %s\r\n",
71               http_request_path (http_request));
72   io_fprintf (io, "The query string was: %s\r\n",
73               http_request_query_string (http_request));
74   io_fprintf (io, "The query arguments were:\r\n");
75
76   params = cgi_params (cgi);
77   for (i = 0; i < vector_size (params); ++i)
78     {
79       vector_get (params, i, name);
80       value = cgi_param (cgi, name);
81       io_fprintf (io, "\t%s=%s\r\n", name, value);
82     }
83
84   io_fprintf (io, "----- end of parameters -----\r\n");
85
86   return close;
87 }