1 karl 1.2.2.2 //%LICENSE////////////////////////////////////////////////////////////////
2 //
3 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 karl 1.2.2.2 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //////////////////////////////////////////////////////////////////////////
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32
33 #include <Pegasus/Common/HTTPConnection.h>
34 #include <Pegasus/Common/Logger.h>
35 #include <Pegasus/Common/Tracer.h>
36
37 #include <Pegasus/WebServer/WebServer.h>
38
39
40 PEGASUS_USING_STD;
41
42 PEGASUS_NAMESPACE_BEGIN
43 karl 1.2.2.2
44 WebServer::WebServer()
45 : MessageQueue("WebServer"),
46 _webProcessor(this)
47 {
48
49 }
50
51
52 WebServer::~WebServer()
53 {
54 }
55
56 void WebServer::enqueue(Message* message)
57 {
58 PEG_METHOD_ENTER(TRC_WEBSERVER,
59 "WebServer::enqueue()");
60 handleEnqueue(message);
61 PEG_METHOD_EXIT();
62 }
63
64 karl 1.2.2.2 void WebServer::handleEnqueue()
65 {
66 PEG_METHOD_ENTER(TRC_WEBSERVER,
67 "WebServer::handleEnqueue()");
68
69 Message* message = dequeue();
70 handleEnqueue(message);
71
72 PEG_METHOD_EXIT();
73 }
74
75 void WebServer::handleEnqueue(Message* message)
76 {
77 PEG_METHOD_ENTER(TRC_WEBSERVER, "WebServer::handleEnqueue()");
78
79 if ( !message )
80 {
81 PEG_METHOD_EXIT();
82 return;
83 }
84
85 karl 1.2.2.2 switch (message->getType())
86 {
87 case HTTP_MESSAGE:
88 handleHTTPMessage((HTTPMessage*)message);
89 break;
90
91 //Handles only the HTTP_MESSAGE, So this is unrechable
92 default:
93 PEGASUS_UNREACHABLE( PEGASUS_ASSERT(0);)
94 break;
95 }
96
97 delete message;
98 PEG_METHOD_EXIT();
99 }
100
101 void WebServer::handleHTTPMessage(HTTPMessage* httpMessage)
102 {
103 PEG_METHOD_ENTER(TRC_WEBSERVER, "WebServer::handleHTTPMessage()");
104
105 if (!httpMessage)
106 karl 1.2.2.2 {
107 PEG_METHOD_EXIT();
108 return;
109 }
110
111 // Parse the HTTP message:
112 String startLine;
113 Uint32 contentLength;
114 Array<HTTPHeader> headers;
115
116 httpMessage->parse(startLine, headers, contentLength);
117
118 // ex.: 'Accept: text/html,application/xhtml+xml,
119 // application/xml;q=0.9,*/*;q=0.8'
120 const char* acceptMimeTypesHeader;
121 Boolean acceptMimeTypesHeaderFound = HTTPMessage::lookupHeader(
122 headers, "Accept", acceptMimeTypesHeader, false);
123
124 //ex.: 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'
125 const char* charSetHeader;
126 Boolean charSetHeaderFound = HTTPMessage::lookupHeader(
127 karl 1.2.2.2 headers, "Accept-Charset", charSetHeader, false);
128
129 //ex.: 'Accept-Encoding: gzip,deflate'
130 //Not supported yet
131 const char* encHeader = NULL;
132 Boolean encHeaderFound = HTTPMessage::lookupHeader(
133 headers, "Accept-Encoding", encHeader, false);
134
135
136 // Parse the request line:
137 String methodName;
138 String requestUri;
139 String httpVersion;
140
141 HTTPMessage::parseRequestLine(
142 startLine, methodName, requestUri, httpVersion);
143
144 PEG_TRACE((TRC_WEBSERVER, Tracer::LEVEL4,
145 "WebServer::handleHTTPMessage(HTTPMessage* httpMessage) - QueueID"
146 " %d: methodName [%s], requestUri [%s], httpVersion [%s],"
147 "mimeTypes [%s], charSets [%s], encoding [%s]",
148 karl 1.2.2.2 httpMessage->queueId,
149 (const char*)methodName.getCString(),
150 (const char*)requestUri.getCString(),
151 (const char*)httpVersion.getCString(),
152 (acceptMimeTypesHeaderFound ? acceptMimeTypesHeader : "NOT FOUND"),
153 (charSetHeaderFound ? charSetHeader : "NOT FOUND"),
154 (encHeaderFound ? encHeader : "NOT FOUND")));
155
156 WebRequest* webRequest = new WebRequest(httpMessage->queueId);
157
158 // set method name
159 if ("GET" == methodName)
160 {
161 webRequest->httpMethod = HTTP_METHOD_GET;
162 }
163 else if ("HEAD" == methodName)
164 {
165 webRequest->httpMethod = HTTP_METHOD_HEAD;
166 }
167
168 if (acceptMimeTypesHeaderFound)
169 karl 1.2.2.2 {
170 webRequest->mimeTypes = String(acceptMimeTypesHeader);
171 }
172
173 if (charSetHeaderFound)
174 {
175 webRequest->charSets = String(charSetHeader);
176 }
177
178 if (encHeaderFound)
179 {
180 webRequest->encodings = String(encHeader);
181 }
182
183 webRequest->requestURI = String(requestUri);
184 webRequest->httpVersion = httpVersion;
185
186 // Save userName and authType:
187 webRequest->userName = httpMessage->authInfo->getAuthenticatedUser();
188 webRequest->authType = httpMessage->authInfo->getAuthType();
189 webRequest->ipAddress = httpMessage->ipAddress;
190 karl 1.2.2.2
191 //for ex.: 'Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'
192 webRequest->acceptLanguages = httpMessage->acceptLanguages;
193
194 /*
195 * TODO async processing in thread.
196 * Take care of userid/authorization
197 */
198 _webProcessor.handleWebRequest(webRequest);
199
200 PEG_METHOD_EXIT();
201 }
202
203
204 void WebServer::handleResponse(HTTPMessage* response)
205 {
206
207 PEG_METHOD_ENTER(TRC_WEBSERVER, "WebServer::handleResponse()");
208
209 Uint32 queueId = response->queueId;
210 MessageQueue* queue = MessageQueue::lookup(queueId);
211 karl 1.2.2.2 AutoPtr<HTTPMessage> httpMessage(response);
212
213 if (!queue)
214 {
215 PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL1,
216 "ERROR: non-existent queueId = %u, response not sent.", queueId));
217 PEG_METHOD_EXIT();
218 return;
219 }
220 PEGASUS_ASSERT(dynamic_cast<HTTPConnection*>(queue) != 0);
221
222 httpMessage->dest = queue->getQueueId();
223 queue->enqueue(httpMessage.release());
224
225 PEG_METHOD_EXIT();
226 }
227
228 PEGASUS_NAMESPACE_END
|