summaryrefslogtreecommitdiffstats
path: root/net/khttpd/rfc.c
blob: ff5e2545115de0d682617a5b7a06cf1f18930e2c (plain)
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
/*

kHTTPd -- the next generation

RFC related functions (headers and stuff)

*/

/****************************************************************
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2, or (at your option)
 *	any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ****************************************************************/


#include <linux/kernel.h>

#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/malloc.h>
#include <linux/net.h>
#include <linux/sched.h>
#include <linux/skbuff.h>
#include <linux/unistd.h>
#include <linux/file.h>
#include <linux/smp_lock.h>

#include <net/ip.h>
#include <net/sock.h>

#include <asm/atomic.h>
#include <asm/semaphore.h>
#include <asm/processor.h>
#include <asm/uaccess.h>


#include "prototypes.h"
#include "structure.h"
#include "sysctl.h"


#define KHTTPD_NUMMIMETYPES 	40

static atomic_t	MimeCount;

struct MimeType
{
	__u32 	identifier;
	char	type[64-sizeof(__u32)-sizeof(__kernel_size_t)];  
	__kernel_size_t	len;
};

static struct MimeType	MimeTypes[KHTTPD_NUMMIMETYPES];


void AddMimeType(const char *Ident,const char *Type)
{	
	__u32	*I;
	
	EnterFunction("AddMimeType");
	
	if (strlen(Ident)!=4) 
   	{	
   		(void)printk(KERN_ERR "httpd: Only 4-byte mime-identifiers are accepted\n");
   		return;
   	}

	if (strlen(Type)>(64-sizeof(__u32)-sizeof(__kernel_size_t) ) )  
   	{	
   		(void)printk(KERN_ERR "httpd: Mime-string too long.\n");
   		return;
   	}
   	
   	I=(__u32*)Ident;
   	
   	/* FIXME: Need to lock-down all access to the mime-structure here */
   	/*        For now, just don't add mime-types after initialisation */
   	
   	
   	MimeTypes[atomic_read(&MimeCount)].identifier=*I;
   	strncpy(MimeTypes[atomic_read(&MimeCount)].type,Type,(64-sizeof(__u32)-sizeof(__kernel_size_t)));
   	MimeTypes[atomic_read(&MimeCount)].len = strlen(Type);
   	
   	atomic_inc(&MimeCount);
   	LeaveFunction("AddMimeType");
}


char *ResolveMimeType(const char *File,__kernel_size_t *Len)
/*

	The returned string is for READ ONLY, ownership of the memory is NOT
	transfered.

*/
{	
	__u32	*I;
	int pos,lc,filelen;
	
	EnterFunction("ResolveMimeType");
	
	*Len = 0;
	
	if (File==NULL)
		return NULL;
	
	filelen = (int)strlen(File);
	
	if (filelen<4) 
   	{	
   		return NULL;
   	}
   	
   	/* The Merced-people are NOT going to like this! So this has to be fixed
   	   in a later stage. */

	pos = filelen-4;
   	I=(__u32*)(File+pos);
   	
   	lc=0;
   	
   	while (lc<atomic_read(&MimeCount))
 	{
   		if (MimeTypes[lc].identifier == *I)
   		{
   			*Len = MimeTypes[lc].len;
   			LeaveFunction("ResolveMimeType - success");
   	  		return MimeTypes[lc].type;
   	  	}
   	  	lc++;
   	} 	
   	
	if (sysctl_khttpd_sloppymime)
	{
		*Len = MimeTypes[0].len;  
		LeaveFunction("ResolveMimeType - unknown");
	   	return MimeTypes[0].type;
	}
	else
	{
		LeaveFunction("ResolveMimeType - failure");
	   	return NULL;
	}
}


static char HeaderPart1[] = "HTTP/1.0 200 OK\r\nServer: kHTTPd/0.1.6\r\nDate: ";
#ifdef BENCHMARK
static char HeaderPart1b[] ="HTTP/1.0 200 OK";
#endif
static char HeaderPart3[] = "\r\nContent-type: ";
static char HeaderPart5[] = "\r\nLast-modified: ";
static char HeaderPart7[] = "\r\nContent-length: ";
static char HeaderPart9[] = "\r\n\r\n";

#ifdef BENCHMARK
/* In BENCHMARK-mode, just send the bare essentials */
void SendHTTPHeader(struct http_request *Request)
{
	struct msghdr	msg;
	mm_segment_t	oldfs;
	struct iovec	iov[9];
	int 		len,len2;
	
	
	EnterFunction("SendHTTPHeader");
		
	msg.msg_name     = 0;
	msg.msg_namelen  = 0;
	msg.msg_iov	 = &iov[0];
	msg.msg_iovlen   = 6;
	msg.msg_control  = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags    = 0;  /* Synchronous for now */
	
	iov[0].iov_base = HeaderPart1b;
	iov[0].iov_len  = 15;
	iov[1].iov_base = HeaderPart3;
	iov[1].iov_len  = 16;
	iov[2].iov_base = Request->MimeType;
	iov[2].iov_len  = Request->MimeLength;
	
	iov[3].iov_base = HeaderPart7;
	iov[3].iov_len  = 18;
	
	
	sprintf(Request->LengthS,"%i",Request->FileLength);
	iov[4].iov_base = Request->LengthS;
	iov[4].iov_len  = strlen(Request->LengthS);
	iov[5].iov_base = HeaderPart9;
	iov[5].iov_len  = 4;
	
	len2=15+16+18+iov[2].iov_len+iov[4].iov_len+4;
	
	
	len = 0;
	

	oldfs = get_fs(); set_fs(KERNEL_DS);
	len = sock_sendmsg(Request->sock,&msg,len2);
	set_fs(oldfs);

	
	return;	
}
#else
void SendHTTPHeader(struct http_request *Request)
{
	struct msghdr	msg;
	mm_segment_t	oldfs;
	struct iovec	iov[9];
	int 		len,len2;
	__kernel_size_t	slen;
	
	EnterFunction("SendHTTPHeader");
	
	msg.msg_name     = 0;
	msg.msg_namelen  = 0;
	msg.msg_iov	 = &(iov[0]);
	msg.msg_iovlen   = 9;
	msg.msg_control  = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags    = 0;  /* Synchronous for now */
	
	iov[0].iov_base = HeaderPart1;
	iov[0].iov_len  = 45;
	iov[1].iov_base = CurrentTime;
	iov[1].iov_len  = 29;
	iov[2].iov_base = HeaderPart3;
	iov[2].iov_len  = 16;
	
	iov[3].iov_base = Request->MimeType;
	iov[3].iov_len  = Request->MimeLength;
	
	iov[4].iov_base = HeaderPart5;
	iov[4].iov_len  = 17;
	iov[5].iov_base = &(Request->TimeS[0]);
	iov[5].iov_len  = 29;
	iov[6].iov_base = HeaderPart7;
	iov[6].iov_len  = 18;
	iov[7].iov_base = &(Request->LengthS[0]);
	slen = strlen(Request->LengthS); 
	iov[7].iov_len  = slen;
	iov[8].iov_base = HeaderPart9;
	iov[8].iov_len  = 4;
	
	len2=45+2*29+16+17+18+slen+4+iov[3].iov_len;
	
	len = 0;

	oldfs = get_fs(); set_fs(KERNEL_DS);
	len = sock_sendmsg(Request->sock,&msg,len2);
	set_fs(oldfs);
	LeaveFunction("SendHTTPHeader");
	

	return;	
}
#endif



/* 

Parse a HTTP-header. Be careful for buffer-overflows here, this is the most important
place for this, since the remote-user controls the data.

*/
void ParseHeader(char *Buffer,const int length, struct http_request *Head)
{
	char *Endval,*EOL,*tmp;
	
	EnterFunction("ParseHeader");
	Endval = Buffer + length;
	
	/* We want to parse only the first header if multiple headers are present */
	tmp = strstr(Buffer,"\r\n\r\n"); 
	if (tmp!=NULL)
	    Endval = tmp;
	
	
	while (Buffer<Endval)
	{
		if (isspace(Buffer[0]))
		{
			Buffer++;
			continue;
		}
			
		
		EOL=strchr(Buffer,'\n');
		
		if (EOL==NULL) EOL=Endval;
		
		if (EOL-Buffer<4) 
		{
			Buffer++;
			continue;
		}
		
		if (strncmp("GET ",Buffer,4)==0)
		{
			int PrefixLen;
			Buffer+=4;
			
			tmp=strchr(Buffer,' ');
			if (tmp==0) 
			{
				tmp=EOL-1;
				Head->HTTPVER = 9;
			} else
				Head->HTTPVER = 10;
			
			if (tmp>Endval) continue;
			
			strncpy(Head->FileName,sysctl_khttpd_docroot,sizeof(Head->FileName));
			PrefixLen = strlen(sysctl_khttpd_docroot);
			Head->FileNameLength = min(255,tmp-Buffer+PrefixLen);		
			
			strncat(Head->FileName,Buffer,min(255-PrefixLen,tmp-Buffer));
					
			Buffer=EOL+1;	
#ifdef BENCHMARK
			break;
#endif						
			continue;
		}
#ifndef BENCHMARK		
		if (strncmp("If-Modified-Since: ",Buffer,19)==0)
		{
			Buffer+=19;
			
			strncpy(Head->IMS,Buffer,min(127,EOL-Buffer-1));
					
			Buffer=EOL+1;	
			continue;
		}

		if (strncmp("User-Agent: ",Buffer,12)==0)
		{
			Buffer+=12;
			
			strncpy(Head->Agent,Buffer,min(127,EOL-Buffer-1));
					
			Buffer=EOL+1;	
			continue;
		}
		

		if (strncmp("Host: ",Buffer,6)==0)
		{
			Buffer+=6;
			
			strncpy(Head->Host,Buffer,min(127,EOL-Buffer-1));
					
			Buffer=EOL+1;	
			continue;
		}
#endif		
		Buffer = EOL+1;  /* Skip line */
	}
	LeaveFunction("ParseHeader");
}