1 //-< CGISTUB.CXX >---------------------------------------------------*--------*
2 // FastDB Version 1.0 (c) 1999 GARRET * ? *
3 // (Main Memory Database Management System) * /\| *
4 // * / \ *
5 // Created: 27-Mar-99 K.A. Knizhnik * / [] \ *
6 // Last update: 30-Jul-99 K.A. Knizhnik * GARRET *
7 //-------------------------------------------------------------------*--------*
8 // Stub for CGI scripts
9 //-------------------------------------------------------------------*--------*
10
11 #include "stdtp.h"
12 #include "sockio.h"
13
14 USE_FASTDB_NAMESPACE
15
message(char const * msg)16 void message(char const* msg)
17 {
18 printf("Content-type: text/html\r\n\r\n"
19 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">"
20 "<HTML><HEAD><TITLE>CGI STUB message</TITLE></HEAD><BODY>"
21 "<H1><FONT COLOR=\"#FF0000\">%s</FONT></H1></BODY></HTML>", msg);
22 exit(0);
23 }
24
25
main()26 int main()
27 {
28 char* request = getenv("QUERY_STRING");
29 char* buf;
30 size_t length;
31 size_t buf_size;
32 char* host = getenv("HTTP_HOST");
33 char* script = getenv("SCRIPT_NAME");
34 char* peer = getenv("REMOTE_ADDR");
35 assert(host != NULL);
36 assert(script != NULL);
37
38 if (request == NULL || *request == '\0') {
39 char* length_str = getenv("CONTENT_LENGTH");
40 if (length_str == NULL || sscanf(length_str, "%d", &length) != 1) {
41 message("Failed to receive data using POST method");
42 }
43 buf_size = length + 14 + 6 + strlen(host) + strlen(script) + strlen(peer);
44 buf = new char[buf_size+1];
45 if (fread(buf, 1, length, stdin) != length) {
46 message("Failed to read data using POST method");
47 }
48 } else {
49 length = strlen(request);
50 buf_size = length + 14 + 6 + strlen(host) + strlen(script) + strlen(peer);
51 buf = new char[buf_size+1];
52 memcpy(buf, request, length);
53 }
54 if (length <= 7 || strncmp(buf, "socket=", 7) != 0) {
55 message("No socket field specified");
56 }
57 sprintf(buf+length, "&stub=http://%s%s&peer=%s&", host, script, peer);
58 char* addr = buf + 7;
59 char *src = addr, *dst = addr;
60 while (*src != '&') {
61 if (*src == '+') {
62 *dst++ = ' ';
63 src += 1;
64 } else if (*src == '%') {
65 *dst++ = ((src[1] >= 'A' ? src[1] - 'A'+ 10 : src[1] - '0') << 4) |
66 (src[2] >= 'A' ? src[2] - 'A'+ 10 : src[2] - '0');
67 src += 3;
68 } else {
69 *dst++ = *src++;
70 }
71 }
72 *dst = '\0';
73 socket_t* s = socket_t::connect(addr);
74 if (s == NULL) {
75 message("Failed to connect to application");
76 }
77 *(int*)buf = (int)buf_size;
78 buf[4] = (char)(src - buf - 3);
79 if (!s->write(buf, (int)buf_size)) {
80 message("Failed to send query to application");
81 }
82 if (s->read(buf, 4, 4) != 4) {
83 message("Failed to read reply length");
84 }
85 nat4 reply_length = *(int4*)buf;
86 if (reply_length > buf_size) {
87 delete[] buf;
88 buf = new char[reply_length];
89 }
90 int size = reply_length - sizeof(reply_length);
91 if (s->read(buf, size, size) != size) {
92 message("Failed to read reply");
93 }
94 fwrite(buf, 1, reply_length - sizeof(reply_length), stdout);
95 delete[] buf;
96 s->close();
97 return 0;
98 }
99
100
101
102