1 /****************************************************************************
2  *                                                                          *
3  * The contents of this file are subject to the WebStone Public License     *
4  * Version 1.0 (the "License"); you may not use this file except in         *
5  * compliance with the License. You may obtain a copy of the License        *
6  * at http://www.mindcraft.com/webstone/license10.html                      *
7  *                                                                          *
8  * Software distributed under the License is distributed on an "AS IS"      *
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See      *
10  * the License for the specific language governing rights and limitations   *
11  * under the License.                                                       *
12  *                                                                          *
13  * The Original Code is WebStone 2.5.                                       *
14  *                                                                          *
15  * The Initial Developer of the Original Code is Silicon Graphics, Inc.     *
16  * and Mindcraft, Inc.. Portions created by Silicon Graphics. and           *
17  * Mindcraft. are Copyright (C) 1995-1998 Silicon Graphics, Inc. and        *
18  * Mindcraft, Inc. All Rights Reserved.                                     *
19  *                                                                          *
20  * Contributor(s): ______________________________________.                  *
21  *                                                                          *
22  ***************************************************************************/
23 
24 /*
25  * Send random bits file
26  * Once this service function is installed, any file with the extension
27  * "dyn-send" will be serviced with this function.  An optional query
28  * string may be passed to alter the amount of data in the response.
29  *
30  * For example:
31  *      /file.dyn-send                  - returns a 10240 byte file
32  *      /file.dyn-send?size=20          - returns a 20 byte file
33  *      /file.dyn-send?size=1024        - returns a 1024 byte file
34  *      etc.
35  *
36  * To install the service routine, compile it as per the makefile
37  * included with your Netscape server distribution (serverroot/nsapi/examples)
38  * and then add the following lines to your netscape server configuration:
39  *
40  * in magnus.conf
41  *      Init fn=load-modules shlib=example.so funcs=nsapi-send
42  *
43  * in obj.conf
44  *      Service method=(GET|HEAD) fn=nsapi-send type=magnus-internal/dyn-send
45  *
46  * in mime.types
47  *      type=magnus-internal/dyn-send        exts=dyn-send
48  *
49  * Mike Belshe
50  * mbelshe@netscape.com
51  * 11-5-95
52  *
53  * NOTE:
54  *    This module contains two bugs:
55  *        1) The array "buffer" is declared with large size (~204800).
56  *           As a result, the web server may crash due to lack of memory.
57  *        2) If the requested size is greater than 204800 (which it is for
58  *	     the standard WebStone file set) than this module may crash the
59  *           web server by overwriting the end of the buffer array.
60  *    We are leaving these two bugs in the code because to fix them would
61  *    change the performance behaviour of this module.
62  *
63  *    Greg Burrell
64  *    Mindcraft, Inc.
65  */
66 
67 #ifndef WIN32
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include "base/pblock.h"
71 #include "base/session.h"
72 #include "frame/protocol.h"
73 #include "base/util.h"
74 #include "frame/http.h"
75 #else
76 #include <windows.h>
77 #define FILE_STDIO 1
78 #endif
79 #include "frame/req.h"
80 
81 #define FILE_SIZE       10240
82 #define HEADERS  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
83 
84 #ifdef WIN32
85 __declspec(dllexport)
86 #endif
87 
nsapi_send(pblock * pb,Session * sn,Request * rq)88 int nsapi_send(pblock *pb, Session *sn, Request *rq)
89 {
90     char *query_string;
91     char buffer[sizeof(HEADERS) + 204800 + 1];
92     int filesize;
93     unsigned int maxindex;
94     unsigned int index;
95 
96     /* Get the query string, if any; check to see if an alternate
97      * file size was specified.
98      */
99     if ( !(query_string = pblock_findval("query", rq->reqpb)) )
100 	filesize = FILE_SIZE;
101     else {
102 	filesize = atoi(&(query_string[5]));
103     }
104 
105     memcpy(&buffer, HEADERS, sizeof(HEADERS)-1);
106 
107     /* Generate the output */
108     maxindex = sizeof(HEADERS) + filesize;
109     for (index=sizeof(HEADERS); index < (maxindex); index++)
110 	/* generate random characters from A-Z */
111 #ifdef IRIX
112 	buffer[index] = rand_r(&maxindex) % 26 + 63;
113 #else
114         buffer[index] = rand() %26 + 63;
115 #endif
116 
117     /* Send the output */
118     if (net_write(sn->csd, buffer, sizeof(HEADERS)-1+filesize) == IO_ERROR)
119 	return REQ_EXIT;
120 
121     return REQ_PROCEED;
122 }
123