1 #include <apt-pkg/strutl.h>
2 #include <apt-pkg/fileutl.h>
3 #include <apt-pkg/error.h>
4 #include <apt-pkg/cmndline.h>
5 #include <apt-pkg/configuration.h>
6 #include <apt-pkg/init.h>
13 #include <sys/socket.h>
14 #include <sys/types.h>
16 #include <netinet/in.h>
23 char const * const httpcodeToStr(int const httpcode
) { /*{{{*/
26 case 100: return "100 Continue";
27 case 101: return "101 Switching Protocols";
29 case 200: return "200 OK";
30 case 201: return "201 Created";
31 case 202: return "202 Accepted";
32 case 203: return "203 Non-Authoritative Information";
33 case 204: return "204 No Content";
34 case 205: return "205 Reset Content";
35 case 206: return "206 Partial Conent";
37 case 300: return "300 Multiple Choices";
38 case 301: return "301 Moved Permanently";
39 case 302: return "302 Found";
40 case 303: return "303 See Other";
41 case 304: return "304 Not Modified";
42 case 305: return "304 Use Proxy";
43 case 307: return "307 Temporary Redirect";
45 case 400: return "400 Bad Request";
46 case 401: return "401 Unauthorized";
47 case 402: return "402 Payment Required";
48 case 403: return "403 Forbidden";
49 case 404: return "404 Not Found";
50 case 405: return "405 Method Not Allowed";
51 case 406: return "406 Not Acceptable";
52 case 407: return "Proxy Authentication Required";
53 case 408: return "Request Time-out";
54 case 409: return "Conflict";
55 case 410: return "Gone";
56 case 411: return "Length Required";
57 case 412: return "Precondition Failed";
58 case 413: return "Request Entity Too Large";
59 case 414: return "Request-URI Too Large";
60 case 415: return "Unsupported Media Type";
61 case 416: return "Requested range not satisfiable";
62 case 417: return "Expectation Failed";
64 case 500: return "Internal Server Error";
65 case 501: return "Not Implemented";
66 case 502: return "Bad Gateway";
67 case 503: return "Service Unavailable";
68 case 504: return "Gateway Time-out";
69 case 505: return "HTTP Version not supported";
74 void addFileHeaders(std::list
<std::string
> &headers
, FileFd
&data
) { /*{{{*/
75 std::ostringstream contentlength
;
76 contentlength
<< "Content-Length: " << data
.FileSize();
77 headers
.push_back(contentlength
.str());
79 std::string
lastmodified("Last-Modified: ");
80 lastmodified
.append(TimeRFC1123(data
.ModificationTime()));
81 headers
.push_back(lastmodified
);
84 void addDataHeaders(std::list
<std::string
> &headers
, std::string
&data
) {/*{{{*/
85 std::ostringstream contentlength
;
86 contentlength
<< "Content-Length: " << data
.size();
87 headers
.push_back(contentlength
.str());
90 bool sendHead(int const client
, int const httpcode
, std::list
<std::string
> &headers
) { /*{{{*/
91 string
response("HTTP/1.1 ");
92 response
.append(httpcodeToStr(httpcode
));
93 headers
.push_front(response
);
95 headers
.push_back("Server: APT webserver");
97 std::string
date("Date: ");
98 date
.append(TimeRFC1123(time(NULL
)));
99 headers
.push_back(date
);
101 std::clog
<< ">>> RESPONSE >>>" << std::endl
;
103 for (std::list
<std::string
>::const_iterator h
= headers
.begin();
104 Success
== true && h
!= headers
.end(); ++h
) {
105 Success
&= FileFd::Write(client
, h
->c_str(), h
->size());
106 Success
&= FileFd::Write(client
, "\r\n", 2);
107 std::clog
<< *h
<< std::endl
;
109 Success
&= FileFd::Write(client
, "\r\n", 2);
110 std::clog
<< "<<<<<<<<<<<<<<<<" << std::endl
;
114 bool sendFile(int const client
, FileFd
&data
) { /*{{{*/
117 unsigned long long actual
= 0;
118 while ((Success
&= data
.Read(buffer
, sizeof(buffer
), &actual
)) == true) {
121 Success
&= FileFd::Write(client
, buffer
, actual
);
123 Success
&= FileFd::Write(client
, "\r\n", 2);
127 bool sendData(int const client
, std::string
const &data
) { /*{{{*/
129 Success
&= FileFd::Write(client
, data
.c_str(), data
.size());
130 Success
&= FileFd::Write(client
, "\r\n", 2);
134 void sendError(int const client
, int const httpcode
, string
const &request
, bool content
) { /*{{{*/
135 std::list
<std::string
> headers
;
136 string
response("<html><head><title>");
137 response
.append(httpcodeToStr(httpcode
)).append("</title></head>");
138 response
.append("<body><h1>").append(httpcodeToStr(httpcode
)).append("</h1");
139 response
.append("This error is a result of the request: <pre>");
140 response
.append(request
).append("</pre></body></html>");
141 addDataHeaders(headers
, response
);
142 sendHead(client
, httpcode
, headers
);
144 sendData(client
, response
);
147 // sendDirectoryLisiting /*{{{*/
148 int filter_hidden_files(const struct dirent
*a
) {
149 if (a
->d_name
[0] == '.')
151 #ifdef _DIRENT_HAVE_D_TYPE
152 // if we have the d_type check that only files and dirs will be included
153 if (a
->d_type
!= DT_UNKNOWN
&&
154 a
->d_type
!= DT_REG
&&
155 a
->d_type
!= DT_LNK
&& // this includes links to regular files
161 int grouped_alpha_case_sort(const struct dirent
**a
, const struct dirent
**b
) {
162 #ifdef _DIRENT_HAVE_D_TYPE
163 if ((*a
)->d_type
== DT_DIR
&& (*b
)->d_type
== DT_DIR
);
164 else if ((*a
)->d_type
== DT_DIR
&& (*b
)->d_type
== DT_REG
)
166 else if ((*b
)->d_type
== DT_DIR
&& (*a
)->d_type
== DT_REG
)
171 struct stat f_prop
; //File's property
172 stat((*a
)->d_name
, &f_prop
);
173 int const amode
= f_prop
.st_mode
;
174 stat((*b
)->d_name
, &f_prop
);
175 int const bmode
= f_prop
.st_mode
;
176 if (S_ISDIR(amode
) && S_ISDIR(bmode
));
177 else if (S_ISDIR(amode
))
179 else if (S_ISDIR(bmode
))
182 return strcasecmp((*a
)->d_name
, (*b
)->d_name
);
184 void sendDirectoryListing(int const client
, string
const &dir
, string
const &request
, bool content
) {
185 std::list
<std::string
> headers
;
186 std::ostringstream listing
;
188 struct dirent
**namelist
;
189 int const counter
= scandir(dir
.c_str(), &namelist
, filter_hidden_files
, grouped_alpha_case_sort
);
191 sendError(client
, 500, request
, content
);
195 listing
<< "<html><head><title>Index of " << dir
<< "</title>"
196 << "<style type=\"text/css\"><!-- td {padding: 0.02em 0.5em 0.02em 0.5em;}"
197 << "tr:nth-child(even){background-color:#dfdfdf;}"
198 << "h1, td:nth-child(3){text-align:center;}"
199 << "table {margin-left:auto;margin-right:auto;} --></style>"
200 << "</head>" << std::endl
201 << "<body><h1>Index of " << dir
<< "</h1>" << std::endl
202 << "<table><tr><th>#</th><th>Name</th><th>Size</th><th>Last-Modified</th></tr>" << std::endl
;
204 listing
<< "<tr><td>d</td><td><a href=\"..\">Parent Directory</a></td><td>-</td><td>-</td></tr>";
205 for (int i
= 0; i
< counter
; ++i
) {
207 std::string
filename(dir
);
208 filename
.append("/").append(namelist
[i
]->d_name
);
209 stat(filename
.c_str(), &fs
);
210 listing
<< "<tr><td>" << ((S_ISDIR(fs
.st_mode
)) ? 'd' : 'f') << "</td>"
211 << "<td><a href=\"" << namelist
[i
]->d_name
<< "\">" << namelist
[i
]->d_name
<< "</a></td>";
212 if (S_ISDIR(fs
.st_mode
))
213 listing
<< "<td>-</td>";
215 listing
<< "<td>" << SizeToStr(fs
.st_size
) << "B</td>";
216 listing
<< "<td>" << TimeRFC1123(fs
.st_mtime
) << "</td></tr>" << std::endl
;
218 listing
<< "</table></body></html>" << std::endl
;
220 std::string
response(listing
.str());
221 addDataHeaders(headers
, response
);
222 sendHead(client
, 200, headers
);
224 sendData(client
, response
);
227 int main(int const argc
, const char * argv
[])
229 CommandLine::Args Args
[] = {
230 {0, "simulate-paywall", "aptwebserver::Simulate-Paywall",
231 CommandLine::Boolean
},
232 {0, "port", "aptwebserver::port", CommandLine::HasArg
},
236 CommandLine
CmdL(Args
, _config
);
237 if(CmdL
.Parse(argc
,argv
) == false) {
238 _error
->DumpErrors();
242 // create socket, bind and listen to it {{{
243 int sock
= socket(AF_INET6
, SOCK_STREAM
, 0);
245 _error
->Errno("aptwerbserver", "Couldn't create socket");
246 _error
->DumpErrors(std::cerr
);
251 int const port
= _config
->FindI("aptwebserver::port", 8080);
252 bool const simulate_broken_server
= _config
->FindB("aptwebserver::Simulate-Paywall", false);
254 // ensure that we accept all connections: v4 or v6
255 int const iponly
= 0;
256 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &iponly
, sizeof(iponly
));
257 // to not linger to an address
258 int const enable
= 1;
259 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
));
261 struct sockaddr_in6 locAddr
;
262 memset(&locAddr
, 0, sizeof(locAddr
));
263 locAddr
.sin6_family
= AF_INET6
;
264 locAddr
.sin6_port
= htons(port
);
265 locAddr
.sin6_addr
= in6addr_any
;
267 if (bind(sock
, (struct sockaddr
*) &locAddr
, sizeof(locAddr
)) < 0) {
268 _error
->Errno("aptwerbserver", "Couldn't bind");
269 _error
->DumpErrors(std::cerr
);
273 if (simulate_broken_server
) {
274 std::clog
<< "Simulating a broken web server that return nonsense "
275 "for all querries" << std::endl
;
277 std::clog
<< "Serving ANY file on port: " << port
<< std::endl
;
283 std::vector
<std::string
> messages
;
285 while ((client
= accept(sock
, NULL
, NULL
)) != -1) {
286 std::clog
<< "ACCEPT client " << client
287 << " on socket " << sock
<< std::endl
;
289 while (ReadMessages(client
, messages
)) {
290 for (std::vector
<std::string
>::const_iterator m
= messages
.begin();
291 m
!= messages
.end(); ++m
) {
292 std::clog
<< ">>> REQUEST >>>>" << std::endl
<< *m
293 << std::endl
<< "<<<<<<<<<<<<<<<<" << std::endl
;
294 std::list
<std::string
> headers
;
295 bool sendContent
= true;
296 if (strncmp(m
->c_str(), "HEAD ", 5) == 0)
298 if (strncmp(m
->c_str(), "GET ", 4) != 0)
299 sendError(client
, 501, *m
, true);
301 std::string host
= LookupTag(*m
, "Host", "");
302 if (host
.empty() == true) {
303 // RFC 2616 ยง14.23 Host
304 sendError(client
, 400, *m
, sendContent
);
308 size_t const filestart
= m
->find(' ', 5);
309 string filename
= m
->substr(5, filestart
- 5);
310 if (filename
.empty() == true)
313 if (simulate_broken_server
== true) {
314 string
data("ni ni ni\n");
315 addDataHeaders(headers
, data
);
316 sendHead(client
, 200, headers
);
317 sendData(client
, data
);
319 else if (RealFileExists(filename
) == true) {
320 FileFd
data(filename
, FileFd::ReadOnly
);
321 std::string condition
= LookupTag(*m
, "If-Modified-Since", "");
322 if (condition
.empty() == false) {
324 if (RFC1123StrToTime(condition
.c_str(), cache
) == true &&
325 cache
>= data
.ModificationTime()) {
326 sendHead(client
, 304, headers
);
330 addFileHeaders(headers
, data
);
331 sendHead(client
, 200, headers
);
332 if (sendContent
== true)
333 sendFile(client
, data
);
335 else if (DirectoryExists(filename
) == true) {
336 sendDirectoryListing(client
, filename
, *m
, sendContent
);
339 sendError(client
, 404, *m
, false);
341 _error
->DumpErrors(std::cerr
);
345 std::clog
<< "CLOSE client " << client
346 << " on socket " << sock
<< std::endl
;