3 kHTTPd -- the next generation
5 Wait for headers on the accepted connections
8 /****************************************************************
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 ****************************************************************/
29 WaitForHeaders polls all connections in "WaitForHeaderQueue" to see if
30 headers have arived. If so, the headers are decoded and the request is
31 moved to either the "SendingDataQueue" or the "UserspaceQueue".
34 The number of requests that changed status
37 #include <linux/config.h>
38 #include <linux/kernel.h>
39 #include <linux/skbuff.h>
40 #include <linux/smp_lock.h>
41 #include <linux/file.h>
43 #include <asm/uaccess.h>
45 #include "structure.h"
46 #include "prototypes.h"
48 static char *Buffer[CONFIG_KHTTPD_NUMCPU];
51 static int DecodeHeader(const int CPUNR, struct http_request *Request);
54 int WaitForHeaders(const int CPUNR)
56 struct http_request *CurrentRequest,**Prev;
60 EnterFunction("WaitForHeaders");
62 CurrentRequest = threadinfo[CPUNR].WaitForHeaderQueue;
64 Prev = &(threadinfo[CPUNR].WaitForHeaderQueue);
66 while (CurrentRequest!=NULL)
69 /* If the connection is lost, remove from queue */
71 if (CurrentRequest->sock->sk->state != TCP_ESTABLISHED
72 && CurrentRequest->sock->sk->state != TCP_CLOSE_WAIT)
74 struct http_request *Next;
76 Next = CurrentRequest->Next;
78 *Prev = CurrentRequest->Next;
79 CurrentRequest->Next = NULL;
82 CleanUpRequest(CurrentRequest);
83 CurrentRequest = Next;
89 /* If data pending, take action */
91 sk = CurrentRequest->sock->sk;
93 if (!skb_queue_empty(&(sk->receive_queue))) /* Do we have data ? */
95 struct http_request *Next;
101 if (DecodeHeader(CPUNR,CurrentRequest)<0)
103 CurrentRequest = CurrentRequest->Next;
108 /* Remove from WaitForHeaderQueue */
110 Next= CurrentRequest->Next;
115 /* Add to either the UserspaceQueue or the DataSendingQueue */
117 if (CurrentRequest->IsForUserspace!=0)
119 CurrentRequest->Next = threadinfo[CPUNR].UserspaceQueue;
120 threadinfo[CPUNR].UserspaceQueue = CurrentRequest;
123 CurrentRequest->Next = threadinfo[CPUNR].DataSendingQueue;
124 threadinfo[CPUNR].DataSendingQueue = CurrentRequest;
127 CurrentRequest = Next;
133 Prev = &(CurrentRequest->Next);
134 CurrentRequest = CurrentRequest->Next;
137 LeaveFunction("WaitForHeaders");
141 void StopWaitingForHeaders(const int CPUNR)
143 struct http_request *CurrentRequest,*Next;
145 EnterFunction("StopWaitingForHeaders");
146 CurrentRequest = threadinfo[CPUNR].WaitForHeaderQueue;
148 while (CurrentRequest!=NULL)
150 Next = CurrentRequest->Next;
151 CleanUpRequest(CurrentRequest);
155 threadinfo[CPUNR].WaitForHeaderQueue = NULL; /* The queue is empty now */
157 free_page((unsigned long)Buffer[CPUNR]);
160 EnterFunction("StopWaitingForHeaders");
166 DecodeHeader peeks at the TCP/IP data, determines what the request is,
167 fills the request-structure and sends the HTTP-header when apropriate.
171 static int DecodeHeader(const int CPUNR, struct http_request *Request)
179 EnterFunction("DecodeHeader");
181 if (Buffer[CPUNR] == NULL) {
182 /* see comments in main.c regarding buffer managemnet - dank */
183 printk(KERN_CRIT "khttpd: lost my buffer");
187 /* First, read the data */
193 msg.msg_control = NULL;
194 msg.msg_controllen = 0;
197 msg.msg_iov->iov_base = &Buffer[CPUNR][0];
198 msg.msg_iov->iov_len = (size_t)4095;
201 oldfs = get_fs(); set_fs(KERNEL_DS);
202 /* 4095 leaves a "0" to terminate the string */
204 len = sock_recvmsg(Request->sock,&msg,4095,MSG_PEEK);
208 /* WONDERFUL. NO COMMENTS. --ANK */
209 Request->IsForUserspace = 1;
213 if (len>=4094) /* BIG header, we cannot decode it so leave it to userspace */
215 Request->IsForUserspace = 1;
219 /* Then, decode the header */
222 ParseHeader(Buffer[CPUNR],len,Request);
224 Request->filp = OpenFileForSecurity(Request->FileName);
227 Request->MimeType = ResolveMimeType(Request->FileName,&Request->MimeLength);
230 if (Request->MimeType==NULL) /* Unknown mime-type */
232 if (Request->filp!=NULL)
235 Request->filp = NULL;
237 Request->IsForUserspace = 1;
242 if (Request->filp==NULL)
244 Request->IsForUserspace = 1;
249 Request->FileLength = (int)Request->filp->f_dentry->d_inode->i_size;
250 Request->Time = Request->filp->f_dentry->d_inode->i_mtime;
251 Request->IMS_Time = mimeTime_to_UnixTime(Request->IMS);
252 sprintf(Request->LengthS,"%i",Request->FileLength);
253 time_Unix2RFC(min_t(unsigned int, Request->Time,CurrentTime_i),Request->TimeS);
254 /* The min() is required by rfc1945, section 10.10:
255 It is not allowed to send a filetime in the future */
257 if (Request->IMS_Time>Request->Time)
258 { /* Not modified since last time */
259 Send304(Request->sock);
260 Request->FileLength=0;
262 else /* Normal Case */
264 Request->sock->sk->tp_pinfo.af_tcp.nonagle = 2; /* this is TCP_CORK */
265 if (Request->HTTPVER!=9) /* HTTP/0.9 doesn't allow a header */
266 SendHTTPHeader(Request);
272 LeaveFunction("DecodeHeader");
277 int InitWaitHeaders(int ThreadCount)
281 EnterFunction("InitWaitHeaders");
283 while (I<ThreadCount)
285 Buffer[I] = (char*)get_free_page((int)GFP_KERNEL);
286 if (Buffer[I] == NULL)
288 printk(KERN_CRIT "kHTTPd: Not enough memory for basic needs\n");
292 free_page( (unsigned long)Buffer[I2++]);
299 LeaveFunction("InitWaitHeaders");