root/gsdl/trunk/src/w32server/wincgiutils.cpp @ 15852

Revision 9598, 4.9 KB (checked in by kjdon, 15 years ago)

added some x++ -> ++x changes submitted by Emanuel Dejanu

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * wincgiutils.cpp
4 * Copyright (C) 1996
5 *
6 * A component of the fnord webserver written by bmorin@wpi.edu.
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 "text_t.h"
29#include <windows.h>
30#include <string.h>
31#include "netio.h"
32#include "httpreq.h"
33#include "wincgiutils.h"
34
35#define default_http_port 80
36
37// Attempt to break a url into segments. If the parsing fails, 'name' is
38// left pointing to the unparsed portion.
39int parse_url(const text_t &url, text_t &protocol, text_t &machine,
40          int *port, text_t &name) {
41
42  protocol.clear();
43  machine.clear();
44  *port = default_http_port;
45  name.clear();
46 
47  text_t::const_iterator begin = url.begin();
48  text_t::const_iterator end = url.end();
49  text_t::const_iterator it = findchar(begin, end, ':');
50  if (it == end) {name = url; return http_error_url_invalid;}
51  if (substr(it+1, it+3) != "//") {name = url; return http_error_url_invalid;}
52  protocol = substr(begin, it);
53  it += 3;
54  while (it < end) {
55    if (*it == ':') {
56      ++it;
57      text_t portstr;
58      while (it != end && (*it >= '0' && *it <= '9')) {
59    portstr.push_back(*it);
60    ++it;
61      }
62      *port = portstr.getint();
63      if (it == end) break;
64    }
65
66    if (*it == '/') {
67      while (it != end) {
68    name.push_back(*it);
69    ++it;
70      }
71      break;
72    }
73
74    machine.push_back(*it);
75    ++it;
76  }
77  return http_ok;
78}
79
80int Send_String(char *str, RequestInfoT *RInfo)
81{
82  return
83    SendData(RInfo->ClientSocket, (BYTE *)str, strlen(str), RInfo->ThreadNum);
84}
85
86int Send_String_N(char *str, int n,  RequestInfoT *RInfo)
87{
88  return
89    SendData(RInfo->ClientSocket, (BYTE *)str, n, RInfo->ThreadNum);
90}
91
92int send_header(char *content, RequestInfoT *RequestInfo)
93{
94  char Header[1536];  int len, lenc;
95  //Build Header
96  Header[0] = 0;
97  //Status Line
98  if (content[0] != '@')          /*No redirection needed*/
99    strcat(Header, "HTTP/1.0 200 OK\r\n");
100  else                            /*Redirection*/
101    strcat(Header, "HTTP/1.0 302 Relocation\r\n");
102  //Server
103  strcat(Header, "Server: GSDL\r\n");
104  //Content Type
105  strcat(Header, "Content-type: ");
106  if (content[0] != '@')          /*No redirection needed*/
107    strcat(Header, content);
108  else {                          /*Redirection entry*/
109    strcat(Header, "text/html \r\n");
110    strcat(Header, "Location: ");
111    len = strlen(Header);
112    lenc = strlen(content) - 1;
113    strncpy(&Header[len], &content[1], lenc);
114    Header[len+lenc] = 0;
115  }
116  strcat(Header, " \r\n");
117  //Single CRLF to end header
118  strcat(Header, "\r\n");
119 
120  return Send_String(Header, RequestInfo);
121}
122
123void send_retrieve_error(int error_num, char *msg1, char *msg2, RequestInfoT *RequestInfo)
124{
125  char Header[512], Body[512], ErrorNumStr[17], BodyLengthStr[17];
126  DWORD BodyLength; int HeaderLength;
127 
128  itoa(error_num, ErrorNumStr, 10);
129 
130  //Build Data (so we can determine length for the header)
131  Body[0] = 0;
132  strcat(Body, "<HTML><HEAD><TITLE>Server Error</TITLE></HEAD><BODY>\n");
133  strcat(Body, "<H1>Error ");
134  strcat(Body, ErrorNumStr);
135  strcat(Body, ": ");
136  strcat(Body, msg1);
137  strcat(Body, "</H1>\n");
138  strcat(Body, msg2);
139  strcat(Body, "\n</BODY></HTML>");
140  BodyLength = (DWORD) strlen(Body);
141
142  //Build Header
143  Header[0] = 0;
144  //Status Line
145  strcat(Header, "HTTP/1.0 ");
146  strcat(Header, ErrorNumStr);
147  strcat(Header, " ");
148  strcat(Header, msg1);
149  strcat(Header, "\r\n");
150  //Server
151  strcat(Header, "Server: GSDL\r\n");
152  strcat(Header, "Content-Type: text/html\r\n");
153  //Content Length
154  itoa(BodyLength, BodyLengthStr, 10);
155  strcat(Header, "Content-Length: ");
156  strcat(Header, BodyLengthStr);
157  strcat(Header, "\r\n");
158  //Single CRLF to end header
159  strcat(Header, "\r\n");
160  HeaderLength = strlen(Header);
161
162   //Send the file if we have one (and if it's not a HEAD request)
163  SendData(RequestInfo->ClientSocket, (BYTE *) Header, strlen(Header), RequestInfo->ThreadNum);
164  //   if ((FileFound == FALSE) && (strcmpi(RequestFields.MethodStr, "HEAD") != 0)) {
165  //Send generated data
166  SendData(RequestInfo->ClientSocket, (BYTE *) Body, BodyLength, RequestInfo->ThreadNum);
167}
Note: See TracBrowser for help on using the browser.