3 #include <apt-pkg/strutl.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/cmndline.h>
7 #include <apt-pkg/configuration.h>
8 #include <apt-pkg/init.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
24 char const * const httpcodeToStr(int const httpcode
) /*{{{*/
29 case 100: return "100 Continue";
30 case 101: return "101 Switching Protocols";
32 case 200: return "200 OK";
33 case 201: return "201 Created";
34 case 202: return "202 Accepted";
35 case 203: return "203 Non-Authoritative Information";
36 case 204: return "204 No Content";
37 case 205: return "205 Reset Content";
38 case 206: return "206 Partial Content";
40 case 300: return "300 Multiple Choices";
41 case 301: return "301 Moved Permanently";
42 case 302: return "302 Found";
43 case 303: return "303 See Other";
44 case 304: return "304 Not Modified";
45 case 305: return "304 Use Proxy";
46 case 307: return "307 Temporary Redirect";
48 case 400: return "400 Bad Request";
49 case 401: return "401 Unauthorized";
50 case 402: return "402 Payment Required";
51 case 403: return "403 Forbidden";
52 case 404: return "404 Not Found";
53 case 405: return "405 Method Not Allowed";
54 case 406: return "406 Not Acceptable";
55 case 407: return "407 Proxy Authentication Required";
56 case 408: return "408 Request Time-out";
57 case 409: return "409 Conflict";
58 case 410: return "410 Gone";
59 case 411: return "411 Length Required";
60 case 412: return "412 Precondition Failed";
61 case 413: return "413 Request Entity Too Large";
62 case 414: return "414 Request-URI Too Large";
63 case 415: return "415 Unsupported Media Type";
64 case 416: return "416 Requested range not satisfiable";
65 case 417: return "417 Expectation Failed";
66 case 418: return "418 I'm a teapot";
68 case 500: return "500 Internal Server Error";
69 case 501: return "501 Not Implemented";
70 case 502: return "502 Bad Gateway";
71 case 503: return "503 Service Unavailable";
72 case 504: return "504 Gateway Time-out";
73 case 505: return "505 HTTP Version not supported";
78 void addFileHeaders(std::list
<std::string
> &headers
, FileFd
&data
) /*{{{*/
80 std::ostringstream contentlength
;
81 contentlength
<< "Content-Length: " << data
.FileSize();
82 headers
.push_back(contentlength
.str());
84 std::string
lastmodified("Last-Modified: ");
85 lastmodified
.append(TimeRFC1123(data
.ModificationTime()));
86 headers
.push_back(lastmodified
);
89 void addDataHeaders(std::list
<std::string
> &headers
, std::string
&data
) /*{{{*/
91 std::ostringstream contentlength
;
92 contentlength
<< "Content-Length: " << data
.size();
93 headers
.push_back(contentlength
.str());
96 bool sendHead(int const client
, int const httpcode
, std::list
<std::string
> &headers
)/*{{{*/
98 std::string
response("HTTP/1.1 ");
99 response
.append(httpcodeToStr(httpcode
));
100 headers
.push_front(response
);
102 headers
.push_back("Server: APT webserver");
104 std::string
date("Date: ");
105 date
.append(TimeRFC1123(time(NULL
)));
106 headers
.push_back(date
);
108 std::clog
<< ">>> RESPONSE >>>" << std::endl
;
110 for (std::list
<std::string
>::const_iterator h
= headers
.begin();
111 Success
== true && h
!= headers
.end(); ++h
)
113 Success
&= FileFd::Write(client
, h
->c_str(), h
->size());
115 Success
&= FileFd::Write(client
, "\r\n", 2);
116 std::clog
<< *h
<< std::endl
;
119 Success
&= FileFd::Write(client
, "\r\n", 2);
120 std::clog
<< "<<<<<<<<<<<<<<<<" << std::endl
;
124 bool sendFile(int const client
, FileFd
&data
) /*{{{*/
128 unsigned long long actual
= 0;
129 while ((Success
&= data
.Read(buffer
, sizeof(buffer
), &actual
)) == true)
134 Success
&= FileFd::Write(client
, buffer
, actual
);
137 Success
&= FileFd::Write(client
, "\r\n", 2);
141 bool sendData(int const client
, std::string
const &data
) /*{{{*/
144 Success
&= FileFd::Write(client
, data
.c_str(), data
.size());
146 Success
&= FileFd::Write(client
, "\r\n", 2);
150 void sendError(int const client
, int const httpcode
, std::string
const &request
,/*{{{*/
151 bool content
, std::string
const &error
= "")
153 std::list
<std::string
> headers
;
154 std::string
response("<html><head><title>");
155 response
.append(httpcodeToStr(httpcode
)).append("</title></head>");
156 response
.append("<body><h1>").append(httpcodeToStr(httpcode
)).append("</h1>");
157 if (error
.empty() == false)
158 response
.append("<p><em>Error</em>: ").append(error
).append("</p>");
159 response
.append("This error is a result of the request: <pre>");
160 response
.append(request
).append("</pre></body></html>");
161 addDataHeaders(headers
, response
);
162 sendHead(client
, httpcode
, headers
);
164 sendData(client
, response
);
167 bool parseFirstLine(int const client
, std::string
const &request
, /*{{{*/
168 std::string
&filename
, bool &sendContent
,
169 bool &closeConnection
)
171 if (strncmp(request
.c_str(), "HEAD ", 5) == 0)
173 if (strncmp(request
.c_str(), "GET ", 4) != 0)
175 sendError(client
, 501, request
, true);
179 size_t const lineend
= request
.find('\n');
180 size_t filestart
= request
.find(' ');
181 for (; request
[filestart
] == ' '; ++filestart
);
182 size_t fileend
= request
.rfind(' ', lineend
);
183 if (lineend
== std::string::npos
|| filestart
== std::string::npos
||
184 fileend
== std::string::npos
|| filestart
== fileend
)
186 sendError(client
, 500, request
, sendContent
, "Filename can't be extracted");
190 size_t httpstart
= fileend
;
191 for (; request
[httpstart
] == ' '; ++httpstart
);
192 if (strncmp(request
.c_str() + httpstart
, "HTTP/1.1\r", 9) == 0)
193 closeConnection
= strcasecmp(LookupTag(request
, "Connection", "Keep-Alive").c_str(), "Keep-Alive") != 0;
194 else if (strncmp(request
.c_str() + httpstart
, "HTTP/1.0\r", 9) == 0)
195 closeConnection
= strcasecmp(LookupTag(request
, "Connection", "Keep-Alive").c_str(), "close") == 0;
198 sendError(client
, 500, request
, sendContent
, "Not a HTTP/1.{0,1} request");
202 filename
= request
.substr(filestart
, fileend
- filestart
);
203 if (filename
.find(' ') != std::string::npos
)
205 sendError(client
, 500, request
, sendContent
, "Filename contains an unencoded space");
208 filename
= DeQuoteString(filename
);
210 // this is not a secure server, but at least prevent the obvious …
211 if (filename
.empty() == true || filename
[0] != '/' ||
212 strncmp(filename
.c_str(), "//", 2) == 0 ||
213 filename
.find_first_of("\r\n\t\f\v") != std::string::npos
||
214 filename
.find("/../") != std::string::npos
)
216 sendError(client
, 400, request
, sendContent
, "Filename contains illegal character (sequence)");
220 // nuke the first character which is a / as we assured above
221 filename
.erase(0, 1);
222 if (filename
.empty() == true)
227 int main(int const argc
, const char * argv
[])
229 CommandLine::Args Args
[] = {
230 {0, "port", "aptwebserver::port", CommandLine::HasArg
},
231 {'c',"config-file",0,CommandLine::ConfigFile
},
232 {'o',"option",0,CommandLine::ArbItem
},
236 CommandLine
CmdL(Args
, _config
);
237 if(CmdL
.Parse(argc
,argv
) == false)
239 _error
->DumpErrors();
243 // create socket, bind and listen to it {{{
244 // ignore SIGPIPE, this can happen on write() if the socket closes connection
245 signal(SIGPIPE
, SIG_IGN
);
246 int sock
= socket(AF_INET6
, SOCK_STREAM
, 0);
249 _error
->Errno("aptwerbserver", "Couldn't create socket");
250 _error
->DumpErrors(std::cerr
);
254 int const port
= _config
->FindI("aptwebserver::port", 8080);
256 // ensure that we accept all connections: v4 or v6
257 int const iponly
= 0;
258 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &iponly
, sizeof(iponly
));
259 // to not linger on an address
260 int const enable
= 1;
261 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
));
263 struct sockaddr_in6 locAddr
;
264 memset(&locAddr
, 0, sizeof(locAddr
));
265 locAddr
.sin6_family
= AF_INET6
;
266 locAddr
.sin6_port
= htons(port
);
267 locAddr
.sin6_addr
= in6addr_any
;
269 if (bind(sock
, (struct sockaddr
*) &locAddr
, sizeof(locAddr
)) < 0)
271 _error
->Errno("aptwerbserver", "Couldn't bind");
272 _error
->DumpErrors(std::cerr
);
276 std::clog
<< "Serving ANY file on port: " << port
<< std::endl
;
281 std::vector
<std::string
> messages
;
283 while ((client
= accept(sock
, NULL
, NULL
)) != -1)
285 std::clog
<< "ACCEPT client " << client
286 << " on socket " << sock
<< std::endl
;
288 while (ReadMessages(client
, messages
))
290 bool closeConnection
= false;
291 for (std::vector
<std::string
>::const_iterator m
= messages
.begin();
292 m
!= messages
.end() && closeConnection
== false; ++m
) {
293 std::clog
<< ">>> REQUEST >>>>" << std::endl
<< *m
294 << std::endl
<< "<<<<<<<<<<<<<<<<" << std::endl
;
295 std::list
<std::string
> headers
;
296 std::string filename
;
297 bool sendContent
= true;
298 if (parseFirstLine(client
, *m
, filename
, sendContent
, closeConnection
) == false)
301 std::string host
= LookupTag(*m
, "Host", "");
302 if (host
.empty() == true)
304 // RFC 2616 §14.23 requires Host
305 sendError(client
, 400, *m
, sendContent
, "Host header is required");
309 if (RealFileExists(filename
) == true)
311 FileFd
data(filename
, FileFd::ReadOnly
);
312 std::string condition
= LookupTag(*m
, "If-Modified-Since", "");
313 if (condition
.empty() == false)
316 if (RFC1123StrToTime(condition
.c_str(), cache
) == true &&
317 cache
>= data
.ModificationTime())
319 sendHead(client
, 304, headers
);
324 addFileHeaders(headers
, data
);
325 sendHead(client
, 200, headers
);
326 if (sendContent
== true)
327 sendFile(client
, data
);
330 sendError(client
, 404, *m
, sendContent
);
332 _error
->DumpErrors(std::cerr
);
334 if (closeConnection
== true)
338 std::clog
<< "CLOSE client " << client
339 << " on socket " << sock
<< std::endl
;