File: RequestHandlingThread.cpp

package info (click to toggle)
between 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 97; perl: 67
file content (394 lines) | stat: -rw-r--r-- 10,227 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Modification History
 *
 * 2002-March-12   Jason Rohrer
 * Created.
 *
 * 2002-April-20   Jason Rohrer
 * Fixed many violations of spec and included support for mime types.
 * Fixed a few bugs.
 *
 * 2002-April-21   Jason Rohrer
 * Fixed a memory leak.
 *
 * 2002-August-2   Jason Rohrer
 * Added use of ConnectionPermissionHandler.
 *
 * 2002-August-5   Jason Rohrer
 * Added another error message.
 * Changed so PageGenerator is responsible for detecting .. parent 
 * directory symbols.
 *
 * 2002-August-7   Jason Rohrer
 * Fixed possible buffer overflows.
 *
 * 2002-September-13   Jason Rohrer
 * Increased buffer size to 5000 bytes
 * (to accommodate long broadcast comments).
 *
 * 2002-October-20   Jason Rohrer
 * Fixed a memory leak of requestBuffer on error.
 *
 * 2003-April-2   Jason Rohrer
 * Changed to specify that the HTTP connection should not be kept alive.
 *
 * 2003-September-5   Jason Rohrer
 * Moved into minorGems.
 */



#include "RequestHandlingThread.h"




RequestHandlingThread::RequestHandlingThread(
    Socket *inSocket, PageGenerator *inGenerator,
    ConnectionPermissionHandler *inConnectionPermissionHandler )
    : mSocket( inSocket ),
      mGenerator( inGenerator ),
      mConnectionPermissionHandler( inConnectionPermissionHandler ),
      mDoneLock( new MutexLock() ), mDone( false ) {

    }



RequestHandlingThread::~RequestHandlingThread() {
    delete mDoneLock;
    }



char RequestHandlingThread::isDone() {
    char tempDone;

    mDoneLock->lock();
    tempDone = mDone;
    mDoneLock->unlock();

    return mDone;
    }



    // example HTTP request and response
    /*
      GET /images/title_homepage4.gif HTTP/1.0

      HTTP/1.0 200 OK
      Date: Fri, 11 May 2001 18:05:08 GMT
      Server: GWS/1.10
      Connection: close
      Expires: Sun, 17 Jan 2038 19:14:07 GMT
      Content-Length: 7963
      Content-Type: image/gif
      Last-Modified: Tue, 21 Nov 2000 16:20:07 GMT
      
      GIF89a1s
    */


void RequestHandlingThread::run() {

    HostAddress *receivedAddress = mSocket->getRemoteHostAddress();

    if( receivedAddress == NULL ) {
        
        printf( "Failed to obtain host address, so "
                "refusing web connection.\n" );
        
        // refuse
        delete mSocket;

        
        // flag that we're done
        mDoneLock->lock();
        mDone = true;
        mDoneLock->unlock();

        return;
        }
    else if(
        ! mConnectionPermissionHandler->isPermitted( receivedAddress ) ) { 

        printf( "Refusing web connection from:  " );
        receivedAddress->print();
        printf( "\n" );

        
        // not permitted
        delete mSocket;

        delete receivedAddress;
        
        // flag that we're done
        mDoneLock->lock();
        mDone = true;
        mDoneLock->unlock();

        return;
        }
    
    // else permitted
    delete receivedAddress;
    

    int maxLength = 5000;
    
    
    // first, receive the request and parse it
    

    SocketStream *sockStream = new SocketStream( mSocket );

    int requestBufferLength = maxLength;
    char *requestBuffer = new char[requestBufferLength];
    int requestBufferIndex = 0;
        
    // read until we see two \r\n 's in a row
    unsigned char *charRead = new unsigned char[1];
    charRead[0] = 0;
    char requestDone = false;

    char error = false;
    
    // _we_ actually only care about the first line of
    // the request, but we need to read the entire request
    // to make the other host happy
    char firstLineDone = false;
    int numRead = 0;

    while( !requestDone && !error ) {
        while( charRead[0] != 13 && !error ) {
            numRead = sockStream->read( charRead, 1 );
                            
            if( !firstLineDone ) {
                if( numRead != 1 ) {
                    error = true;
                    sendBadRequest( sockStream );
                    }
                // read data into our buffer
                else if( requestBufferIndex < requestBufferLength ) {
                    requestBuffer[ requestBufferIndex ] =
                        charRead[0];
                    requestBufferIndex++;

                    if( charRead[0] == 13 ) {
                        firstLineDone = true;
                        }
                    }
                else {
                    error = true;
                    sendBadRequest( sockStream );
                    }
                }
            }
        if( !error ) {
            // look for rest of double \r\n
            // this will effectively skip other lines in the request,
            // since we don't care about them
            numRead = sockStream->read( charRead, 1 );

            if( charRead[0] == 10 && numRead == 1 ) {

                numRead = sockStream->read( charRead, 1 );
                if( charRead[0] == 13 && numRead == 1  ) {

                    numRead = sockStream->read( charRead, 1 );
                    if( charRead[0] == 10 && numRead == 1 ) {
                        requestDone = true;
                        }
                    }
                }

            if( numRead != 1 ) {
                error = true;
                sendBadRequest( sockStream );
                }
            }
        }

    // \0 terminate the request buffer
    if( requestBufferIndex < requestBufferLength ) {
        requestBuffer[ requestBufferIndex ] = '\0';
        }
    else {
        requestBuffer[ requestBufferLength - 1 ] = '\0';
        }
    
    if( !error ) {
        // at this point, we have received the entire
        // request, and stored the most important part in
        // requestBuffer


        // if maxLength = 500,
        // formatString = "%499s"
        // used to limit length of scanned string 
        char *formatString = new char[ 20 ];
        sprintf( formatString, "%%%ds", maxLength - 1 );
        
        
        // the second string scanned from the buffer should
        // be the file path requested
        
        char *filePathBuffer = new char[ maxLength ];
        int numRead = sscanf( requestBuffer, formatString, filePathBuffer );

        if( numRead != 1 || strcmp( filePathBuffer, "GET" ) != 0 ) {
            // an invalid request
            error = true;
            sendBadRequest( sockStream );
            }
        else {
            // a proper GET request

            // skip the GET and read the file name
            numRead = sscanf( &( requestBuffer[3] ),
                              formatString, filePathBuffer );
        
            if( numRead != 1 ) {
                error = true;
                sendBadRequest( sockStream );
                }
            }
        
        delete [] requestBuffer;
        delete [] charRead;


        delete [] formatString;
        
        if( !error ) {
            // now we have the requested file string
            sockStream->writeString(
                "HTTP/1.0 200 OK\r\n" );

            char *mimeType = mGenerator->getMimeType( filePathBuffer );

            sockStream->writeString( "Content-Type: " );
            sockStream->writeString( mimeType );
            sockStream->writeString( "\r\n" );
            
            delete [] mimeType;

            // even if the client requests a keep-alive, we force a close
            sockStream->writeString( "Connection: close" );
            
            // finish header
            sockStream->writeString( "\r\n\r\n" );
            
            // pass it to our page generator, which will send the content
            mGenerator->generatePage( filePathBuffer, sockStream );
            }

        delete [] filePathBuffer;  
        }
    else {
        delete [] requestBuffer;
        delete [] charRead;
        }

    
    delete sockStream;
    delete mSocket;

    
    // flag that we're done
    mDoneLock->lock();
    mDone = true;
    mDoneLock->unlock();
    }



void RequestHandlingThread::sendNotFoundPage(
    SocketStream *inStream,
    char *inFileName ) {

    // example "not found" response
    /*
      HTTP/1.0 404 Not Found
      Date: Fri, 11 May 2001 18:26:16 GMT
      Server: GWS/1.10
      Connection: close
      Set-Cookie: PREF=ID=3300d4623bf73a57:TM=989605576:LM=989605576;
          domain=.google.com; path=/; expires=Sun, 17-Jan-2038 19:14:07 GMT
      Content-Length: 142
      Content-Type: text/html

      <HTML><HEAD><TITLE>Not Found</TITLE></HEAD>
      <BODY><H1>404 Not Found</H1>
      The requested URL /fjfj was not found on this server.
      </BODY></HTML>
    */
    char *buffer = new char[500];

    if( inFileName != NULL ) {
        sprintf( buffer,
                 "HTTP/1.0 404 Not Found\r\n\r\n<HTML>"
                 "<BODY><H1>404 Not Found</H1>The requested file "
                 "<b>%s</b> was not found</BODY></HTML>\r\n",
                 inFileName );
        }
    else {
        sprintf( buffer,
                 "HTTP/1.0 404 Not Found\r\n\r\n<HTML>"
                 "<BODY><H1>404 Not Found</H1>The requested "
                 "file was not found</BODY></HTML>\r\n" );
        }

    inStream->write( (unsigned char *)buffer, strlen( buffer ) );

    delete [] buffer;
    }



void RequestHandlingThread::sendBadRequest(
    SocketStream *inStream ) {

    // exampl "bad request" response
    /*
      <HTML><HEAD><TITLE>Bad Request</TITLE></HEAD>
      <BODY><H1>400 Bad Request</H1>
      Your client has issued a malformed or illegal request.
      </BODY></HTML>
    */

    char *buffer = new char[500];

    sprintf( buffer,
             "<HTML><BODY><H1>400 Bad Request</H1>"
             "Your client has issued a malformed or illegal request."
             "</BODY></HTML>\r\n" );

    inStream->write( (unsigned char *)buffer, strlen( buffer ) );

    delete [] buffer;
    }



char* RequestHandlingThread::getTimestamp() {
    char *stampBuffer = new char[99];

    time_t t = time( NULL );
    
    char *asciiTime = ctime( &t );

    // this time string ends with a newline...
    // get rid of it
    asciiTime[ strlen(asciiTime) - 1 ] = '\0';
    
    
    sprintf( stampBuffer, "[%s]", asciiTime );

    // delete [] asciiTime;
    
    return stampBuffer;
    }