Basically Working

This commit is contained in:
2018-05-18 17:22:01 +02:00
parent 164eda05fe
commit 0c2590b3f5
6 changed files with 460 additions and 12 deletions

View File

@ -5,27 +5,135 @@
*/
#include "necronda-server.h"
#include <magic.h>
#include <iostream>
#include "network/Connection.cpp"
#include "network/http/HttpHeader.cpp"
#include <thread>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
/**
* Returns UNIX time in microseconds
* @return UNIX time [µs]
*/
unsigned long getMicros() {
struct timeval tv;
gettimeofday(&tv, nullptr);
return (unsigned long) (1000000 * tv.tv_sec + tv.tv_usec);
}
string getMimeType(string path) {
unsigned long pos = path.find_last_of('.');
string ext;
if (pos != string::npos) {
ext = path.substr(pos + 1, path.length() - pos);
}
magic_t magic = magic_open(MAGIC_MIME_TYPE);
magic_load(magic, "/usr/share/misc/magic.mgc");
string type = magic_file(magic, path.c_str());
magic_setflags(magic, MAGIC_MIME_ENCODING);
string charset = magic_file(magic, path.c_str());
if (type == "text/plain") {
if (ext == "css") {
type = "text/css";
} else if (ext == "js") {
type = "text/javascript";
}
}
return type + "; charset=" + charset;
}
/**
* Sun, 06 Nov 1994 08:49:37 GMT
* @return
*/
string getHttpDate() {
time_t rawtime;
time(&rawtime);
return getHttpDate(rawtime);
}
string getHttpDate(string filename) {
struct stat attrib;
stat(filename.c_str(), &attrib);
return getHttpDate(attrib.st_ctime);
}
string getHttpDate(time_t time) {
char buffer[64];
struct tm *timeinfo = gmtime(&time);
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", timeinfo);
return string(buffer);
}
/**
* Returns a formatted time string
* @param micros Delta time to be formatted
* @return A formatted time string
*/
std::string formatTime(long micros) {
char buffer[64];
if (micros < 1000) {
sprintf(buffer, "%.3f ms", micros / 1000.0);
} else if (micros < 10000) {
sprintf(buffer, "%.2f ms", micros / 1000.0);
} else if (micros < 100000) {
sprintf(buffer, "%.1f ms", micros / 1000.0);
} else if (micros < 1000000) {
sprintf(buffer, "%.0f ms", micros / 1000.0);
} else {
sprintf(buffer, "%.1f s", micros / 1000000.0);
}
return std::string(buffer);
}
string getWebRoot(string host) {
return "/home/lorenz/Documents/Projects/Necronda-Server/necronda-server-3.0/webroot";
}
#include "network/Address.cpp"
#include "network/Socket.cpp"
#include "Path.cpp"
#include "network/http/HttpStatusCode.cpp"
#include "network/http/HttpHeader.cpp"
#include "network/http/HttpRequest.cpp"
#include "network/http/HttpResponse.cpp"
#include "network/http/HttpConnection.cpp"
#include "client.cpp"
long clientnum = 0;
int main() {
cout << "Necronda Server 3.0" << endl << "by Lorenz Stechauner" << endl << endl;
unsigned short PORT = 8080;
Connection *s;
Socket *s;
try {
s = new Connection();
s = new Socket();
} catch (char *msg) {
cout << "Unable to create socket: " << msg << endl;
exit(1);
}
try {
s->setReuseAddress(true);
} catch (char *msg) {
cout << "Unable to set socket option: " << msg << endl;
exit(1);
}
try {
s->bind(PORT);
} catch (char *msg) {
@ -42,9 +150,9 @@ int main() {
while (true) {
try {
Connection *client = s->accept();
cout << client->getPeerAddress()->toString() << ":" << client->getPeerPort() << " <-> "
<< client->getSocketAddress()->toString() << ":" << client->getSocketPort() << endl;
Socket *socket = s->accept();
clientnum++;
thread *t = new thread(client_handler, socket, clientnum);
} catch (char *msg) {
cout << msg << endl;
break;
@ -54,3 +162,4 @@ int main() {
return 0;
}