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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS HTTP Daemon
 * FILE:        httpd.cpp
 * PURPOSE:     HTTP daemon
 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
 * REVISIONS:
 *   CSH  01/09/2000 Created
 */
#include <debug.h>
#include <new>
#include <malloc.h>
#include <string.h>
#include <config.h>
#include <httpd.h>
#include <error.h>

using namespace std;

CHAR HttpMsg400[] = "<HEAD><TITLE>400 Bad Request</TITLE></HEAD>\n\r<BODY><H1>400 Bad Request</H1>\n\rThe request had bad syntax.<BR>\n\r</BODY>\n\r\n\r";
CHAR HttpMsg404[] = "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n\r<BODY><H1>404 Not Found</H1>\n\rThe requested URL was not found on this server.<BR>\n\r</BODY>\n\r\n\r";
CHAR HttpMsg405[] = "<HEAD><TITLE>405 Method Not Allowed</TITLE></HEAD>\n\r<BODY><H1>405 Method Not Allowed</H1>\n\rThe requested method is not supported on this server.<BR>\n\r</BODY>\n\r\n\r";
CHAR HttpMsg500[] = "<HEAD><TITLE>500 Internal Server Error</TITLE></HEAD>\n\r<BODY><H1>500 Internal Server Error</H1>\n\rAn internal error occurred.<BR>\n\r</BODY>\n\r\n\r";
CHAR HttpMsg501[] = "<HEAD><TITLE>501 Not Implemented</TITLE></HEAD>\n\r<BODY><H1>501 Not Implemented</H1>\n\rThe feature is not implemented.<BR>\n\r</BODY>\n\r\n\r";


// *************************** CHttpClient ***************************

// Default constructor
CHttpClient::CHttpClient()
{
}

// Constructor with server socket as starter value
CHttpClient::CHttpClient(CServerSocket *serversocket)
{
	ServerSocket = serversocket;
}

// Split URIs into its parts (ie. |http://|www.host.com|/resource|?parameters|)
VOID CHttpClient::SplitUri(LPSTR lpsUri, LPSTR lpsHost, LPSTR lpsResource, LPSTR lpsParams)
{
    LPSTR lpsPos;
    LPSTR lpsStr;
    UINT i;

	strcpy(lpsHost, "");
	strcpy(lpsResource, "");
	strcpy(lpsParams, "");

    lpsPos = strstr(lpsUri, "://");
    if (lpsPos != NULL)
        lpsStr = &lpsPos[3];
	else
		lpsStr = lpsUri;

    lpsPos = strstr(lpsStr, "/");
    if (lpsPos != NULL) {
        strncat(lpsHost, lpsPos, lpsPos - lpsStr);
        lpsStr = &lpsPos[1];

        lpsPos = strstr(lpsStr, "?");
        if (lpsPos != NULL) {
            strncat(lpsResource, lpsStr, lpsPos - lpsStr);
            strcpy(lpsParams, &lpsPos[1]);
        } else {
            strcpy(lpsResource, lpsStr);
            strcpy(lpsParams, "");
        }

        // Replace "/" with "\"
		for (i = 0; i < strlen(lpsResource); i++) {
            if (lpsResource[i] == '/')
                lpsResource[i] = '\\';
        }
    }
}

// Split resource into its parts (ie. |/path/|filename|.extension|)
VOID CHttpClient::SplitResource(LPSTR lpsResource, LPSTR lpsPath, LPSTR lpsFilename, LPSTR lpsExtension)
{
    INT i,len,fileptr,extptr;

	strcpy(lpsPath, "");
	strcpy(lpsFilename, "");
	strcpy(lpsExtension, "");

	len = strlen(lpsResource);
	if (len != 0) {
		if (lpsResource[len - 1] == '/') {
			// There is only a path
			strcpy(lpsPath, lpsResource);
		} else {
			// Find extension
			i = len - 1;
			while ((i >= 0) && (lpsResource[i] != '.')) i--;
			extptr = i;
			while ((i >= 0) && (lpsResource[i] != '/')) i--;
			if (i > 0) {
				// There is at least one directory in the path (besides root directory)
				fileptr = i + 1;
				strncat(lpsPath, lpsResource, fileptr);
			} else
				fileptr = 1;

			// Get filename and possibly extension
			if (extptr != 0) {
				strncat(lpsFilename, &lpsResource[fileptr], extptr - fileptr);
				// Get extension
				strncat(lpsExtension, &lpsResource[extptr + 1], len - extptr - 1);
			} else
				strncat(lpsFilename, &lpsResource[fileptr], len - fileptr);
		}
	}
}

// Process HTTP request
VOID CHttpClient::ProcessRequest()
{
    CHAR sStr[255];
    CHAR sHost[255];
    CHAR sResource[255];
    CHAR sParams[255];

    // Which method?
    switch (Parser.nMethodNo) {
		case hmGET: {
			SplitUri(Parser.sUri, sHost, sResource, sParams);

			// Default resource?
			if (strlen(sResource) == 0) {
				CIterator<LPSTR> *i = pConfiguration->GetDefaultResources()->CreateIterator();

				// FIXME: All default resources should be tried
				// Iterate through all strings
				//for (i->First(); !i->IsDone(); i->Next())
				i->First();
				if (!i->IsDone()) {
					strcat(sResource, i->CurrentItem());
					delete i;
				} else {
					// File not found
					Report("404 Not Found", HttpMsg404);
					break;
				}
			}
			strcpy(sStr, pConfiguration->GetHttpBase());
			strcat(sStr, sResource);
			SendFile(sStr);
			break;
		}
		default: {
			// Method is not implemented
			Report("501 Not Implemented", HttpMsg501);
		}
	}
}

// Send a file to socket
VOID CHttpClient::SendFile(LPSTR lpsFilename)
{
    CHAR str[255];
    CHAR str2[32];
    union BigNum {
      //        unsigned __int64 Big;
      unsigned long long Big;
        struct {
            DWORD Low;
            DWORD High;
        } u;
    } nTotalBytes;
	DWORD nBytesToRead;
	DWORD nBytesRead;
	BOOL bStatus;

	// Try to open file
    hFile = CreateFileA(lpsFilename,
        GENERIC_READ,               // Open for reading
        FILE_SHARE_READ,            // Share for reading
        NULL,                       // No security
        OPEN_EXISTING,              // Existing file only
        FILE_ATTRIBUTE_NORMAL,      // Normal file
        NULL);                      // No attr. template
    if (hFile == INVALID_HANDLE_VALUE) {
        // File not found
        Report("404 Not Found", HttpMsg404);
        return;
    }
    // Get file size
    nTotalBytes.u.Low = GetFileSize(hFile, &nTotalBytes.u.High);
    if ((nTotalBytes.u.Low == 0xFFFFFFFF) && ((GetLastError()) != NO_ERROR)) {
        // Internal server error
		Report("500 Internal Server Error", HttpMsg500);
		// Close file
		CloseHandle(hFile);
        return;
    }

	// Determine buffer size
	if (nTotalBytes.Big < 65536)
		nBufferSize = 1024;
	else
		nBufferSize = 32768;
	// Allocate memory on heap
	lpsBuffer = (PCHAR) malloc(nBufferSize);

	if (lpsBuffer == NULL) {
		// Internal server error
		Report("500 Internal Server Error", HttpMsg500);
		// Close file
		CloseHandle(hFile);
		return;
	}

    SendText("HTTP/1.1 200 OK");
    SendText("Server: ROSHTTPD");
    SendText("MIME-version: 1.0");
    SendText("Content-Type: text/plain");
    SendText("Accept-Ranges: bytes");
    strcpy(str, "Content-Length: ");
    _itoa(nTotalBytes.u.Low, str2, 10);
    strcat(str, str2);
    SendText(str);
    SendText("");
	// Read and transmit file
	nTotalRead = 0;
	nFileSize = nTotalBytes.Big;
	bStop = FALSE;

	fd_set wfds;
	FD_ZERO(&wfds);
	FD_SET(Socket, &wfds);
	do {
		MessageLoop();

		if (nTotalRead + nBufferSize < nFileSize)
			nBytesToRead = nBufferSize;
		else nBytesToRead = nFileSize - nTotalRead;

		bStatus = ReadFile(hFile, lpsBuffer, nBytesToRead, &nBytesRead, NULL);
		if (bStatus) {
			select(0, NULL, &wfds, NULL, NULL);
			bStatus = (Transmit(lpsBuffer, nBytesRead) == (INT)nBytesRead);
			nTotalRead += nBytesRead;
		}
    } while ((!bStop) && (bStatus) && (nTotalRead < nFileSize));

	if (bStatus)
		SendText("");
	else
		// We can't send an error message here as we are in the process of sending a file.
		// We have to terminate the connection instead
		Close();

	// Free allocated memory
	free(lpsBuffer);

	// Close file
    CloseHandle(hFile);
}

// Report something to client
VOID CHttpClient::Report(LPCSTR lpsCode, LPSTR lpsStr)
{
    CHAR sTmp[128];
    CHAR sTmp2[16];

    strcpy(sTmp, "HTTP/1.1 ");
    strcat(sTmp, lpsCode);
    SendText(sTmp);
    SendText("Server: ROSHTTPD");
    SendText("MIME-version: 1.0");
    SendText("Content-Type: text/html");
    SendText("Accept-Ranges: bytes");
    strcpy(sTmp, "Content-Length: ");
    if (lpsStr != NULL) {
        _itoa(strlen(lpsStr), sTmp2, 10);
        strcat(sTmp,  sTmp2);
    } else
        strcat(sTmp, "0");
    SendText(sTmp);
    SendText("");
    if (lpsStr != NULL)
        SendText(lpsStr);
    SendText("");
}

// OnRead event handler
VOID CHttpClient::OnRead()
{
	LONG nCount;

	nCount = Receive((LPSTR) &Parser.sBuffer[Parser.nHead],
        sizeof(Parser.sBuffer) - Parser.nHead);

    Parser.nHead += nCount;
	if (Parser.nHead >= sizeof(Parser.sBuffer))
		Parser.nHead = 0;

    if (Parser.Complete()) {
		ProcessRequest();
    }

	if (Parser.bUnknownMethod) {
		// Method Not Allowed
		Report("405 Method Not Allowed", HttpMsg405);
		// Terminate connection
		Close();
	}
}
/*
// OnWrite event handler
VOID CHttpClient::OnWrite()
{
	DWORD nBytesToRead;
	DWORD nBytesRead;

	OutputDebugString(TS("Can write\n"));

	if (bSendingFile) {
		if (nTotalRead + nBufferSize < nFileSize)
			nBytesToRead = nBufferSize;
		else nBytesToRead = nFileSize - nTotalRead;

		bError = ReadFile(hFile, Buffer, nBytesToRead, &nBytesRead, NULL);
		if (!bError) {
			Transmit(Buffer, nBytesRead);
			nTotalRead += nBytesRead;
		}
	}
}
*/
// OnClose event handler
VOID CHttpClient::OnClose()
{
	// Stop sending file if we are doing that now
	bStop = TRUE;
}


// ************************ CHttpClientThread ************************

// Constructor with client socket as starter value
CHttpClientThread::CHttpClientThread(LPCServerClientSocket lpSocket)
{
	ClientSocket = lpSocket;
}

// Execute client thread code
VOID CHttpClientThread::Execute()
{
    MSG Msg;

	while (!Terminated()) {
		((  CHttpClient *) ClientSocket)->MessageLoop();
        if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE) != 0) {
            switch (Msg.message) {
                case HTTPD_START: {
					// TODO: Start thread
                    break;
                }
                case HTTPD_STOP: {
					// TODO: Stop thread
                    break;
                }
                default:
                    DispatchMessage(&Msg);
            }

        }
    }

	if (ClientSocket != NULL) {
		delete ClientSocket;
		ClientSocket = NULL;
	}
}


// *************************** CHttpDaemon ***************************

// Default constructor
CHttpDaemon::CHttpDaemon()
{
    State = hsStopped;
	Start();
}

// Default destructor
CHttpDaemon::~CHttpDaemon()
{
	if (State==hsRunning)
		Stop();
}

// Return daemon state
HTTPdState CHttpDaemon::GetState() const
{
	return State;
}

// Start HTTP daemon
BOOL CHttpDaemon::Start()
{
	assert(State==hsStopped);

	SetPort(pConfiguration->GetPort());

	Open();

	State = hsRunning;

    return TRUE;
}

// Stop HTTP daemon
BOOL CHttpDaemon::Stop()
{
	assert(State==hsRunning);

	Close();

	State = hsStopped;

    return TRUE;
}

// OnGetSocket event handler
LPCServerClientSocket CHttpDaemon::OnGetSocket(LPCServerSocket lpServerSocket)
{
	return new CHttpClient(lpServerSocket);
}

// OnGetThread event handler
LPCServerClientThread CHttpDaemon::OnGetThread(LPCServerClientSocket lpSocket)
{
	return new CHttpClientThread(lpSocket);
}

// OnAccept event handler
VOID CHttpDaemon::OnAccept(LPCServerClientThread lpThread)
{
}


// ************************ CHttpDaemonThread ************************

// Execute daemon thread code
VOID CHttpDaemonThread::Execute()
{
	MSG Msg;

	try {
		Daemon = NULL;
		Daemon = new CHttpDaemon;

		while (!Terminated()) {
			Daemon->MessageLoop();
			if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE) != 0) {
	            switch (Msg.message) {
		            case HTTPD_START: {
			            if (Daemon->GetState() == hsStopped)
				            Daemon->Start();
                    break;
					}
					case HTTPD_STOP: {
	                    if (Daemon->GetState() == hsRunning)
							Daemon->Stop();
                    break;
					}
					case HTTPD_SUSPEND: {
						if (Daemon->GetState() == hsRunning){}
							// FIXME: Suspend service
                    break;
					}
					case HTTPD_RESUME: {
						if (Daemon->GetState() != hsSuspended){}
							// FIXME: Resume service
                    break;
					}
					default:
                        DispatchMessage(&Msg);
				}
			}
	    }
		delete Daemon;
	} catch (ESocket e) {
		ReportErrorStr(e.what());
	} catch	(bad_alloc&) {
		ReportErrorStr(TS("Insufficient resources."));
	}
}