version 0.1.6
[fms.git] / libs / shttpd / io_cgi.c
1 /*
2  * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3  * All rights reserved
4  *
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * Sergey Lyubka wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.
9  */
10
11 #include "defs.h"
12
13 static int
14 write_cgi(struct stream *stream, const void *buf, size_t len)
15 {
16         assert(stream->chan.sock != -1);
17         assert(stream->flags & FLAG_W);
18
19         return (send(stream->chan.sock, buf, len, 0));
20 }
21
22 static int
23 read_cgi(struct stream *stream, void *buf, size_t len)
24 {
25         struct headers  parsed;
26         char            status[4];
27         int             n;
28
29         assert(stream->chan.sock != -1);
30         assert(stream->flags & FLAG_R);
31
32         stream->flags &= ~FLAG_DONT_CLOSE;
33
34         n = recv(stream->chan.sock, buf, len, 0);
35
36         if (stream->flags & FLAG_HEADERS_PARSED)
37                 return (n);
38
39         if (n <= 0 && ERRNO != EWOULDBLOCK) {
40                 send_server_error(stream->conn, 500, "Error running CGI");
41                 return (n);
42         }
43
44         /*
45          * CGI script may output Status: and Location: headers, which
46          * may alter the status code. Buffer in headers, parse
47          * them, send correct status code and then forward all data
48          * from CGI script back to the remote end.
49          * Reply line was alredy appended to the IO buffer in
50          * decide_what_to_do(), with blank status code.
51          */
52
53         stream->flags |= FLAG_DONT_CLOSE;
54         io_inc_head(&stream->io, n);
55
56         stream->headers_len = get_headers_len(stream->io.buf, stream->io.head);
57         if (stream->headers_len < 0) {
58                 stream->flags &= ~FLAG_DONT_CLOSE;
59                 send_server_error(stream->conn, 500, "Bad headers sent");
60                 elog(E_LOG, stream->conn, "CGI script sent invalid headers: "
61                     "[%.*s]", stream->io.head - CGI_REPLY_LEN,
62                     stream->io.buf + CGI_REPLY_LEN);
63                 return (0);
64         }
65
66         /*
67          * If we did not received full headers yet, we must not send any
68          * data read from the CGI back to the client. Suspend sending by
69          * setting tail = head, which tells that there is no data in IO buffer
70          */
71
72         if (stream->headers_len == 0) {
73                 stream->io.tail = stream->io.head;
74                 return (0);
75         }
76
77         /* Received all headers. Set status code for the connection. */
78         (void) memset(&parsed, 0, sizeof(parsed));
79         parse_headers(stream->io.buf, stream->headers_len, &parsed);
80         stream->content_len = parsed.cl.v_big_int;
81         stream->conn->status = (int) parsed.status.v_big_int;
82
83         /* If script outputs 'Location:' header, set status code to 302 */
84         if (parsed.location.v_vec.len > 0)
85                 stream->conn->status = 302;
86
87         /*
88          * If script did not output neither 'Location:' nor 'Status' headers,
89          * set the default status code 200, which means 'success'.
90          */
91         if (stream->conn->status == 0)
92                 stream->conn->status = 200;
93
94         /* Append the status line to the beginning of the output */
95         (void) my_snprintf(status, sizeof(status), "%3d", stream->conn->status);
96         (void) memcpy(stream->io.buf + 9, status, 3);
97         DBG(("read_cgi: content len %lu status %s",
98             stream->content_len, status));
99
100         /* Next time, pass output directly back to the client */
101         assert((big_int_t) stream->headers_len <= stream->io.total);
102         stream->io.total -= stream->headers_len;
103         stream->io.tail = 0;
104         stream->flags |= FLAG_HEADERS_PARSED;
105
106         /* Return 0 because we've already shifted the head */
107         return (0);
108 }
109
110 static void
111 close_cgi(struct stream *stream)
112 {
113         assert(stream->chan.sock != -1);
114         (void) closesocket(stream->chan.sock);
115 }
116
117 const struct io_class   io_cgi =  {
118         "cgi",
119         read_cgi,
120         write_cgi,
121         close_cgi
122 };