#include <utility>
#include <vector>
+#include "accesslog.h"
#include "log.h"
#include "markpool.h"
#include "mutexlock.h"
using namespace std;
+extern AccessLogThread *access_log;
+
Server::Server()
{
pthread_mutex_init(&mutex, NULL);
epoll_fd = epoll_create(1024); // Size argument is ignored.
if (epoll_fd == -1) {
- perror("epoll_fd");
+ log_perror("epoll_fd");
exit(1);
}
}
Server::~Server()
{
+ for (map<string, Stream *>::iterator stream_it = streams.begin();
+ stream_it != streams.end();
+ ++stream_it) {
+ delete stream_it->second;
+ }
+
int ret;
do {
ret = close(epoll_fd);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close(epoll_fd)");
+ log_perror("close(epoll_fd)");
}
}
continue;
}
if (nfds == -1) {
- perror("epoll_wait");
+ log_perror("epoll_wait");
exit(1);
}
ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
ev.data.u64 = reinterpret_cast<uint64_t>(&clients[sock]);
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_ADD)");
+ log_perror("epoll_ctl(EPOLL_CTL_ADD)");
exit(1);
}
ev.data.u64 = 0; // Keep Valgrind happy.
ev.data.u64 = reinterpret_cast<uint64_t>(client_ptr);
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_ADD)");
+ log_perror("epoll_ctl(EPOLL_CTL_ADD)");
exit(1);
}
return;
}
if (ret == -1) {
- perror("read");
+ log_perror("read");
close_client(client);
return;
}
switch (status) {
case RP_OUT_OF_SPACE:
- log(WARNING, "fd %d sent overlong request!", client->sock);
+ log(WARNING, "[%s] Client sent overlong request!", client->remote_addr.c_str());
close_client(client);
return;
case RP_NOT_FINISHED_YET:
// See if there's more data for us.
goto read_request_again;
case RP_EXTRA_DATA:
- log(WARNING, "fd %d had junk data after request!", client->sock);
+ log(WARNING, "[%s] Junk data after request!", client->remote_addr.c_str());
close_client(client);
return;
case RP_FINISHED:
if (ret == -1) {
// Error! Postcondition #1.
- perror("write");
+ log_perror("write");
close_client(client);
return;
}
return;
}
if (bytes_to_send > stream->backlog_size) {
- log(WARNING, "fd %d lost %lld bytes, maybe too slow connection",
- client->sock,
+ log(WARNING, "[%s] Client lost %lld bytes, maybe too slow connection",
+ client->remote_addr.c_str(),
(long long int)(bytes_to_send - stream->backlog_size));
client->stream_pos = stream->bytes_received - stream->backlog_size;
client->bytes_lost += bytes_to_send - stream->backlog_size;
}
if (ret == -1) {
// Error, close; postcondition #1.
- perror("sendfile");
+ log_perror("sendfile");
close_client(client);
return;
}
}
if (setsockopt(client->sock, SOL_SOCKET, SO_MARK, &client->fwmark, sizeof(client->fwmark)) == -1) {
if (client->fwmark != 0) {
- perror("setsockopt(SO_MARK)");
+ log_perror("setsockopt(SO_MARK)");
}
}
client->request.clear();
ev.data.u64 = reinterpret_cast<uint64_t>(client);
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_MOD)");
+ log_perror("epoll_ctl(EPOLL_CTL_MOD)");
exit(1);
}
}
ev.data.u64 = reinterpret_cast<uint64_t>(client);
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_MOD)");
+ log_perror("epoll_ctl(EPOLL_CTL_MOD)");
exit(1);
}
}
void Server::close_client(Client *client)
{
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
- perror("epoll_ctl(EPOLL_CTL_DEL)");
+ log_perror("epoll_ctl(EPOLL_CTL_DEL)");
exit(1);
}
}
}
+ // Log to access_log.
+ access_log->write(client->get_stats());
+
// Bye-bye!
int ret;
do {
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close");
+ log_perror("close");
}
clients.erase(client->sock);