1 /* Pseudothread echo example.
2 * - by Richard W.M. Jones <rich@annexia.org>
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.
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.
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.
18 * $Id: pthr_eg1_echo.c,v 1.6 2002/12/01 14:57:36 rich Exp $
25 #ifdef HAVE_SYS_SOCKET_H
26 #include <sys/socket.h>
35 #include "src/pthr_pseudothread.h"
36 #include "src/pthr_iolib.h"
37 #include "src/pthr_http.h"
38 #include "src/pthr_cgi.h"
39 #include "pthr_eg1_echo.h"
41 struct eg1_echo_processor
43 /* Pseudothread handle. */
50 static void run (void *vp);
53 new_eg1_echo_processor (int sock)
59 p = pmalloc (pool, sizeof *p);
62 p->pth = new_pseudothread (pool, run, p, "eg1_echo_processor");
72 eg1_echo_processor p = (eg1_echo_processor) vp;
74 http_request http_request;
76 http_response http_response;
77 pool pool = pth_get_pool (p->pth);
81 io = io_fdopen (p->sock);
83 /* Sit in a loop reading HTTP requests. */
86 vector headers, params;
87 struct http_header header;
88 const char *name, *value;
90 /* ----- HTTP request ----- */
91 http_request = new_http_request (pool, io);
92 if (http_request == 0) /* Normal end of file. */
95 cgi = new_cgi (pool, http_request, io);
96 if (cgi == 0) /* XXX Should send an error here. */
99 /* ----- HTTP response ----- */
100 http_response = new_http_response (pool, http_request,
103 http_response_send_header (http_response,
104 "Content-Type", "text/plain");
105 close = http_response_end_headers (http_response);
107 if (!http_request_is_HEAD (http_request))
109 io_fprintf (io, "Hello. This is your server.\r\n\r\n");
110 io_fprintf (io, "Your browser sent the following headers:\r\n");
112 headers = http_request_get_headers (http_request);
113 for (i = 0; i < vector_size (headers); ++i)
115 vector_get (headers, i, header);
116 io_fprintf (io, "\t%s: %s\r\n", header.key, header.value);
119 io_fprintf (io, "----- end of headers -----\r\n");
121 io_fprintf (io, "The URL was: %s\r\n",
122 http_request_get_url (http_request));
123 io_fprintf (io, "The path component was: %s\r\n",
124 http_request_path (http_request));
125 io_fprintf (io, "The query string was: %s\r\n",
126 http_request_query_string (http_request));
127 io_fprintf (io, "The query arguments were:\r\n");
129 params = cgi_params (cgi);
130 for (i = 0; i < vector_size (params); ++i)
132 vector_get (params, i, name);
133 value = cgi_param (cgi, name);
134 io_fprintf (io, "\t%s=%s\r\n", name, value);
137 io_fprintf (io, "----- end of parameters -----\r\n");