2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
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.
14 write_cgi(struct stream *stream, const void *buf, size_t len)
16 assert(stream->chan.sock != -1);
17 assert(stream->flags & FLAG_W);
19 return (send(stream->chan.sock, buf, len, 0));
23 read_cgi(struct stream *stream, void *buf, size_t len)
25 struct headers parsed;
29 assert(stream->chan.sock != -1);
30 assert(stream->flags & FLAG_R);
32 stream->flags &= ~FLAG_DONT_CLOSE;
34 n = recv(stream->chan.sock, buf, len, 0);
36 if (stream->flags & FLAG_HEADERS_PARSED)
39 if (n <= 0 && ERRNO != EWOULDBLOCK) {
40 send_server_error(stream->conn, 500, "Error running CGI");
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.
53 stream->flags |= FLAG_DONT_CLOSE;
54 io_inc_head(&stream->io, n);
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);
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
72 if (stream->headers_len == 0) {
73 stream->io.tail = stream->io.head;
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;
83 /* If script outputs 'Location:' header, set status code to 302 */
84 if (parsed.location.v_vec.len > 0)
85 stream->conn->status = 302;
88 * If script did not output neither 'Location:' nor 'Status' headers,
89 * set the default status code 200, which means 'success'.
91 if (stream->conn->status == 0)
92 stream->conn->status = 200;
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));
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;
104 stream->flags |= FLAG_HEADERS_PARSED;
106 /* Return 0 because we've already shifted the head */
111 close_cgi(struct stream *stream)
113 assert(stream->chan.sock != -1);
114 (void) closesocket(stream->chan.sock);
117 const struct io_class io_cgi = {