1 | #include <windows.h>
|
---|
2 | #include <string.h>
|
---|
3 | #include "netio.h"
|
---|
4 | #include "httpreq.h"
|
---|
5 | // #include "locate.h"
|
---|
6 | #include "wincgiutils.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | #define url_limit 256
|
---|
10 | #define file_limit 102400 /*should be large enough for a header line*/
|
---|
11 | #define default_http_port 80
|
---|
12 |
|
---|
13 |
|
---|
14 | int parse_url(char *url,
|
---|
15 | char **protocol, char **machine, int *port, char **name)
|
---|
16 | /* Attempt to break a url into segments. If the parsing
|
---|
17 | fails, 'name' is left pointing to the unparsed portion.
|
---|
18 | This routine may extend the url string by one character */
|
---|
19 | {
|
---|
20 | char next, thiss;
|
---|
21 | *protocol = *machine = "";
|
---|
22 | *port = default_http_port;
|
---|
23 |
|
---|
24 | *name = url; /* In case there is no protocol */
|
---|
25 | while (*url != ':' && *url != 0) url++;
|
---|
26 | if (*url == 0) return http_error_url_invalid;
|
---|
27 | url++;
|
---|
28 | if (*url != '/') return http_error_url_invalid;
|
---|
29 | *url++ = 0;
|
---|
30 | if (*url != '/') return http_error_url_invalid;
|
---|
31 | url++;
|
---|
32 |
|
---|
33 | *protocol = *name;
|
---|
34 | *machine = url;
|
---|
35 | while (*url != ':' && *url != '/' && *url != 0) url++;
|
---|
36 | if (*url == ':') {
|
---|
37 | *url = 0;
|
---|
38 | url++; *port = 0;
|
---|
39 | while (*url >= '0' && *url <= '9')
|
---|
40 | *port = *port * 10 + (*url++ - '0');
|
---|
41 | }
|
---|
42 | if (*url == '/') {
|
---|
43 | *url++ = 0;
|
---|
44 | next = '/';
|
---|
45 | *name = url;
|
---|
46 | while (*url != 0) {
|
---|
47 | thiss = *url; *url++ = next; next = thiss; }
|
---|
48 | *url++ = next;
|
---|
49 | *url = 0;
|
---|
50 | }
|
---|
51 | else if (*url == 0) {
|
---|
52 | url++;
|
---|
53 | *name = url;
|
---|
54 | *url++ = '/';
|
---|
55 | *url = 0;
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | *name = url;
|
---|
59 | return http_error_url_invalid;
|
---|
60 | }
|
---|
61 | return http_ok;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | int Send_String(char *str, RequestInfoT *RInfo)
|
---|
68 | {
|
---|
69 | return
|
---|
70 | SendData(RInfo->ClientSocket, (BYTE *)str, strlen(str), RInfo->ThreadNum);
|
---|
71 | }
|
---|
72 |
|
---|
73 | int Send_String_N(char *str, int n, RequestInfoT *RInfo)
|
---|
74 | {
|
---|
75 | return
|
---|
76 | SendData(RInfo->ClientSocket, (BYTE *)str, n, RInfo->ThreadNum);
|
---|
77 | }
|
---|
78 |
|
---|
79 | int send_header(char *content, RequestInfoT *RequestInfo)
|
---|
80 | {
|
---|
81 | char Header[1536]; int len, lenc;
|
---|
82 | //Build Header
|
---|
83 | Header[0] = 0;
|
---|
84 | //Status Line
|
---|
85 | if (content[0] != '@') /*No redirection needed*/
|
---|
86 | strcat(Header, "HTTP/1.0 200 OK\r\n");
|
---|
87 | else /*Redirection*/
|
---|
88 | strcat(Header, "HTTP/1.0 302 Relocation\r\n");
|
---|
89 | //Server
|
---|
90 | strcat(Header, "Server: GSDL\r\n");
|
---|
91 | //Content Type
|
---|
92 | strcat(Header, "Content-type: ");
|
---|
93 | if (content[0] != '@') /*No redirection needed*/
|
---|
94 | strcat(Header, content);
|
---|
95 | else { /*Redirection entry*/
|
---|
96 | strcat(Header, "text/html \r\n");
|
---|
97 | strcat(Header, "Location: ");
|
---|
98 | len = strlen(Header);
|
---|
99 | lenc = strlen(content) - 1;
|
---|
100 | strncpy(&Header[len], &content[1], lenc);
|
---|
101 | Header[len+lenc] = 0;
|
---|
102 | }
|
---|
103 | strcat(Header, " \r\n");
|
---|
104 | //Single CRLF to end header
|
---|
105 | strcat(Header, "\r\n");
|
---|
106 |
|
---|
107 | return Send_String(Header, RequestInfo);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void send_retrieve_error(int error_num, char *msg1, char *msg2, RequestInfoT *RequestInfo)
|
---|
111 | {
|
---|
112 | char Header[512], Body[512], ErrorNumStr[17], BodyLengthStr[17];
|
---|
113 | DWORD BodyLength; int HeaderLength;
|
---|
114 |
|
---|
115 | itoa(error_num, ErrorNumStr, 10);
|
---|
116 |
|
---|
117 | //Build Data (so we can determine length for the header)
|
---|
118 | Body[0] = 0;
|
---|
119 | strcat(Body, "<HTML><HEAD><TITLE>Server Error</TITLE></HEAD><BODY>\n");
|
---|
120 | strcat(Body, "<H1>Error ");
|
---|
121 | strcat(Body, ErrorNumStr);
|
---|
122 | strcat(Body, ": ");
|
---|
123 | strcat(Body, msg1);
|
---|
124 | strcat(Body, "</H1>\n");
|
---|
125 | strcat(Body, msg2);
|
---|
126 | strcat(Body, "\n</BODY></HTML>");
|
---|
127 | BodyLength = (DWORD) strlen(Body);
|
---|
128 |
|
---|
129 | //Build Header
|
---|
130 | Header[0] = 0;
|
---|
131 | //Status Line
|
---|
132 | strcat(Header, "HTTP/1.0 ");
|
---|
133 | strcat(Header, ErrorNumStr);
|
---|
134 | strcat(Header, " ");
|
---|
135 | strcat(Header, msg1);
|
---|
136 | strcat(Header, "\r\n");
|
---|
137 | //Server
|
---|
138 | strcat(Header, "Server: GSDL\r\n");
|
---|
139 | strcat(Header, "Content-Type: text/html\r\n");
|
---|
140 | //Content Length
|
---|
141 | itoa(BodyLength, BodyLengthStr, 10);
|
---|
142 | strcat(Header, "Content-Length: ");
|
---|
143 | strcat(Header, BodyLengthStr);
|
---|
144 | strcat(Header, "\r\n");
|
---|
145 | //Single CRLF to end header
|
---|
146 | strcat(Header, "\r\n");
|
---|
147 | HeaderLength = strlen(Header);
|
---|
148 |
|
---|
149 | //Send the file if we have one (and if it's not a HEAD request)
|
---|
150 | SendData(RequestInfo->ClientSocket, (BYTE *) Header, strlen(Header), RequestInfo->ThreadNum);
|
---|
151 | // if ((FileFound == FALSE) && (strcmpi(RequestFields.MethodStr, "HEAD") != 0)) {
|
---|
152 | //Send generated data
|
---|
153 | SendData(RequestInfo->ClientSocket, (BYTE *) Body, BodyLength, RequestInfo->ThreadNum);
|
---|
154 |
|
---|
155 | }
|
---|
156 |
|
---|