1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * wincgiutils.cpp
|
---|
4 | * Copyright (C) 1996
|
---|
5 | *
|
---|
6 | * A component of the fnord webserver written by [email protected].
|
---|
7 | *
|
---|
8 | * Altered for use with the Greenstone digital library software by the
|
---|
9 | * New Zealand Digital Library Project at the University of Waikato,
|
---|
10 | * New Zealand.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | #include <windows.h>
|
---|
29 | #include <string.h>
|
---|
30 | #include "netio.h"
|
---|
31 | #include "httpreq.h"
|
---|
32 | #include "wincgiutils.h"
|
---|
33 |
|
---|
34 | #define default_http_port 80
|
---|
35 |
|
---|
36 | // Attempt to break a url into segments. If the parsing fails, 'name' is
|
---|
37 | // left pointing to the unparsed portion.
|
---|
38 | int parse_url(const text_t &url, text_t &protocol, text_t &machine,
|
---|
39 | int *port, text_t &name) {
|
---|
40 |
|
---|
41 | protocol.clear();
|
---|
42 | machine.clear();
|
---|
43 | *port = default_http_port;
|
---|
44 | name.clear();
|
---|
45 |
|
---|
46 | text_t::const_iterator begin = url.begin();
|
---|
47 | text_t::const_iterator end = url.end();
|
---|
48 | text_t::const_iterator it = findchar(begin, end, ':');
|
---|
49 | if (it == end) {name = url; return http_error_url_invalid;}
|
---|
50 | if (substr(it+1, it+3) != "//") {name = url; return http_error_url_invalid;}
|
---|
51 | protocol = substr(begin, it);
|
---|
52 | it += 3;
|
---|
53 | while (it < end) {
|
---|
54 | if (*it == ':') {
|
---|
55 | it++;
|
---|
56 | text_t portstr;
|
---|
57 | while (it != end && (*it >= '0' && *it <= '9')) {
|
---|
58 | portstr.push_back(*it);
|
---|
59 | it++;
|
---|
60 | }
|
---|
61 | *port = portstr.getint();
|
---|
62 | if (it == end) break;
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (*it == '/') {
|
---|
66 | while (it != end) {
|
---|
67 | name.push_back(*it);
|
---|
68 | it++;
|
---|
69 | }
|
---|
70 | break;
|
---|
71 | }
|
---|
72 |
|
---|
73 | machine.push_back(*it);
|
---|
74 | it ++;
|
---|
75 | }
|
---|
76 | return http_ok;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int Send_String(char *str, RequestInfoT *RInfo)
|
---|
80 | {
|
---|
81 | return
|
---|
82 | SendData(RInfo->ClientSocket, (BYTE *)str, strlen(str), RInfo->ThreadNum);
|
---|
83 | }
|
---|
84 |
|
---|
85 | int Send_String_N(char *str, int n, RequestInfoT *RInfo)
|
---|
86 | {
|
---|
87 | return
|
---|
88 | SendData(RInfo->ClientSocket, (BYTE *)str, n, RInfo->ThreadNum);
|
---|
89 | }
|
---|
90 |
|
---|
91 | int send_header(char *content, RequestInfoT *RequestInfo)
|
---|
92 | {
|
---|
93 | char Header[1536]; int len, lenc;
|
---|
94 | //Build Header
|
---|
95 | Header[0] = 0;
|
---|
96 | //Status Line
|
---|
97 | if (content[0] != '@') /*No redirection needed*/
|
---|
98 | strcat(Header, "HTTP/1.0 200 OK\r\n");
|
---|
99 | else /*Redirection*/
|
---|
100 | strcat(Header, "HTTP/1.0 302 Relocation\r\n");
|
---|
101 | //Server
|
---|
102 | strcat(Header, "Server: GSDL\r\n");
|
---|
103 | //Content Type
|
---|
104 | strcat(Header, "Content-type: ");
|
---|
105 | if (content[0] != '@') /*No redirection needed*/
|
---|
106 | strcat(Header, content);
|
---|
107 | else { /*Redirection entry*/
|
---|
108 | strcat(Header, "text/html \r\n");
|
---|
109 | strcat(Header, "Location: ");
|
---|
110 | len = strlen(Header);
|
---|
111 | lenc = strlen(content) - 1;
|
---|
112 | strncpy(&Header[len], &content[1], lenc);
|
---|
113 | Header[len+lenc] = 0;
|
---|
114 | }
|
---|
115 | strcat(Header, " \r\n");
|
---|
116 | //Single CRLF to end header
|
---|
117 | strcat(Header, "\r\n");
|
---|
118 |
|
---|
119 | return Send_String(Header, RequestInfo);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void send_retrieve_error(int error_num, char *msg1, char *msg2, RequestInfoT *RequestInfo)
|
---|
123 | {
|
---|
124 | char Header[512], Body[512], ErrorNumStr[17], BodyLengthStr[17];
|
---|
125 | DWORD BodyLength; int HeaderLength;
|
---|
126 |
|
---|
127 | itoa(error_num, ErrorNumStr, 10);
|
---|
128 |
|
---|
129 | //Build Data (so we can determine length for the header)
|
---|
130 | Body[0] = 0;
|
---|
131 | strcat(Body, "<HTML><HEAD><TITLE>Server Error</TITLE></HEAD><BODY>\n");
|
---|
132 | strcat(Body, "<H1>Error ");
|
---|
133 | strcat(Body, ErrorNumStr);
|
---|
134 | strcat(Body, ": ");
|
---|
135 | strcat(Body, msg1);
|
---|
136 | strcat(Body, "</H1>\n");
|
---|
137 | strcat(Body, msg2);
|
---|
138 | strcat(Body, "\n</BODY></HTML>");
|
---|
139 | BodyLength = (DWORD) strlen(Body);
|
---|
140 |
|
---|
141 | //Build Header
|
---|
142 | Header[0] = 0;
|
---|
143 | //Status Line
|
---|
144 | strcat(Header, "HTTP/1.0 ");
|
---|
145 | strcat(Header, ErrorNumStr);
|
---|
146 | strcat(Header, " ");
|
---|
147 | strcat(Header, msg1);
|
---|
148 | strcat(Header, "\r\n");
|
---|
149 | //Server
|
---|
150 | strcat(Header, "Server: GSDL\r\n");
|
---|
151 | strcat(Header, "Content-Type: text/html\r\n");
|
---|
152 | //Content Length
|
---|
153 | itoa(BodyLength, BodyLengthStr, 10);
|
---|
154 | strcat(Header, "Content-Length: ");
|
---|
155 | strcat(Header, BodyLengthStr);
|
---|
156 | strcat(Header, "\r\n");
|
---|
157 | //Single CRLF to end header
|
---|
158 | strcat(Header, "\r\n");
|
---|
159 | HeaderLength = strlen(Header);
|
---|
160 |
|
---|
161 | //Send the file if we have one (and if it's not a HEAD request)
|
---|
162 | SendData(RequestInfo->ClientSocket, (BYTE *) Header, strlen(Header), RequestInfo->ThreadNum);
|
---|
163 | // if ((FileFound == FALSE) && (strcmpi(RequestFields.MethodStr, "HEAD") != 0)) {
|
---|
164 | //Send generated data
|
---|
165 | SendData(RequestInfo->ClientSocket, (BYTE *) Body, BodyLength, RequestInfo->ThreadNum);
|
---|
166 | }
|
---|