11#include <netinet/in.h>
12#include <sys/socket.h>
42 cout <<
"Images of the event display will be saved in directory: " <<
outputDir << endl;
50 cout <<
"MpdWebScreenshots::Init()" << endl;
90 gEve->GetDefaultGLViewer()->SavePicture(fileName.
Data());
95 gEve->GetDefaultGLViewer()->SavePicture(fileName.
Data());
135 if ((chdir(
"/")) < 0)
143 return send(
socket, message, strlen(message), 0);
148 char* head = (
char*)
"\r\nHTTP/1.1 ";
149 char* content_head = (
char*)
"\r\nContent-Type: ";
150 char* server_head = (
char*)
"\r\nServer: PT06";
151 char* length_head = (
char*)
"\r\nContent-Length: ";
152 char* date_head = (
char*)
"\r\nDate: ";
153 char* newline = (
char*)
"\r\n";
158 char contentLength[100];
159 sprintf(contentLength,
"%i", TotalSize);
161 char* message = (
char*)malloc((strlen(head) + strlen(content_head) + strlen(server_head) + strlen(length_head)
162 + strlen(date_head) + strlen(newline) + strlen(Status_code) + strlen(Content_Type)
163 + strlen(contentLength) + 28 +
sizeof(
char))
166 if (message != NULL) {
167 strcpy(message, head);
168 strcat(message, Status_code);
169 strcat(message, content_head);
170 strcat(message, Content_Type);
171 strcat(message, server_head);
172 strcat(message, length_head);
173 strcat(message, contentLength);
174 strcat(message, date_head);
175 strcat(message, (
char*)ctime(&rawtime));
176 strcat(message, newline);
187 int current_char = 0;
189 current_char = fgetc(fp);
191 send(connecting_socket, ¤t_char,
sizeof(
char), 0);
192 }
while (current_char != EOF);
197 if (
start >= strlen(input))
200 int appending_char_count = 0;
204 for (;
i < strlen(input);
i++) {
205 if (*(input +
i) !=
'\t' && *(input +
i) !=
' ' && *(input +
i) !=
'\n' && *(input +
i) !=
'\r') {
206 if (count < (
max - 1)) {
207 *(output + appending_char_count) = *(input +
i);
208 appending_char_count += 1;
214 *(output + appending_char_count) =
'\0';
219 for (;
i < strlen(input);
i++) {
220 if (*(input +
i) !=
'\t' && *(input +
i) !=
' ' && *(input +
i) !=
'\n' && *(input +
i) !=
'\r')
229 char* current_word = (
char*)malloc(600);
230 char* word_holder = (
char*)malloc(600);
231 char* line = (
char*)malloc(200);
234 TString mime_file =
"mime.types";
235 FILE* mimeFile = fopen(mime_file.
Data(),
"r");
238 mime_type = (
char*)malloc(200);
240 memset(mime_type,
'\0', 200);
242 while (fgets(line, 200, mimeFile) != NULL) {
243 if (line[0] !=
'#') {
244 startline =
scan(line, current_word, 0, 600);
246 startline =
scan(line, word_holder, startline, 600);
247 if (startline != -1) {
248 if (strcmp(word_holder, extension) == 0) {
249 memcpy(mime_type, current_word, strlen(current_word));
272 char* filename = (
char*)malloc(100);
273 int start =
scan(input, filename, 4, 100);
276 output[strlen(output) + 1] =
'\0';
278 if (strcmp(
"HTTP/1.1", output) == 0)
281 if (strcmp(
"HTTP/1.0", output) == 0)
296 int appended_position = 0;
298 size_t i = 0, count = 0;
299 for (;
i < strlen(input);
i++) {
300 if (in_position == 1) {
302 output[appended_position] = input[
i];
303 appended_position += 1;
311 output[appended_position + 1] =
'\0';
313 if (strlen(output) > 0)
322 char* filename = (
char*)malloc(200 *
sizeof(
char));
323 char* path = (
char*)malloc(1000 *
sizeof(
char));
324 char* extension = (
char*)malloc(10 *
sizeof(
char));
325 char* mime = (
char*)malloc(200 *
sizeof(
char));
326 char* httpVersion = (
char*)malloc(20 *
sizeof(
char));
328 int contentLength = 0, mimeSupported = 0;
331 memset(filename,
'\0', 200);
332 memset(extension,
'\0', 10);
334 memset(httpVersion,
'\0', 20);
336 int fileNameLength =
scan(input, filename, 5, 200);
339 while (filename[
i] !=
'\0' && filename[
i] !=
'?')
342 if (filename[
i] ==
'?')
345 if (fileNameLength <= 0)
349 sendString(
"501 Not Implemented\n", connecting_socket);
354 printf(
"File extension not existing");
355 sendString(
"400 Bad Request\n", connecting_socket);
364 mimeSupported =
checkMime(extension, mime);
365 if (mimeSupported != 1) {
366 printf(
"Mime not supported");
367 sendString(
"400 Bad Request\n", connecting_socket);
377 strcpy(path, output_dir.
Data());
378 strcat(path, filename);
380 FILE* fp = fopen(path,
"rb");
382 printf(
"\nUnable to open file %s\n", path);
384 sendString(
"404 Not Found\n", connecting_socket);
394 fseek(fp, 0, SEEK_END);
395 contentLength = ftell(fp);
397 if (contentLength < 0) {
398 printf(
"File size is zero");
409 sendHeader(
"200 OK", mime, contentLength, connecting_socket);
426 if (strlen(input) > 0)
429 char* requestType = (
char*)malloc(5);
430 scan(input, requestType, 0, 5);
432 if (type == 1 && strcmp(
"GET", requestType) == 0)
434 else if (type == 1 && strcmp(
"HEAD", requestType) == 0)
436 else if (strlen(input) > 4 && strcmp(
"POST", requestType) == 0)
449 if ((recv(connecting_socket, buffer,
BUFFER_SIZE, 0)) == -1) {
450 printf(
"Error handling incoming request");
457 else if (request != 2)
460 sendString(
"501 Not Implemented\n", connecting_socket);
462 sendString(
"400 Bad Request\n", connecting_socket);
472 sockaddr_storage connectorSocket;
473 socklen_t addressSize =
sizeof(connectorSocket);
475 int connecting_socket = accept(currentSocket, (
struct sockaddr*)&(connectorSocket), &addressSize);
476 if (connecting_socket < 0) {
477 perror(
"Accepting sockets");
486 if (
receive(connecting_socket, output_dir) < 0) {
491 close(connecting_socket);
493 while (-1 != waitpid(-1, NULL, WNOHANG))
502 int currentSocket =
socket(AF_INET, SOCK_STREAM, 0);
503 if (currentSocket == -1) {
504 perror(
"Create socket");
509 struct sockaddr_in address;
510 address.sin_family = AF_INET;
511 address.sin_addr.s_addr = INADDR_ANY;
512 address.sin_port = htons(webPort);
514 if (bind(currentSocket, (
struct sockaddr*)&address,
sizeof(address)) < 0) {
515 perror(
"Bind to port");
521 perror(
"Listen on port");
540 for (
int parameterCount = 1; parameterCount < argc; parameterCount++) {
542 if (strcmp(argv[parameterCount],
"-p") == 0) {
545 printf(
"Setting port to %i\n", atoi(argv[parameterCount]));
546 webPort = atoi(argv[parameterCount]);
551 if (strcmp(argv[parameterCount],
"-d") == 0) {
552 printf(
"Setting daemon = TRUE");
555 if (strcmp(argv[parameterCount],
"-l") == 0) {
558 printf(
"Setting logfile = %s\n", argv[parameterCount]);
559 log_file = (
TString)(
char*)argv[parameterCount];
561 printf(
"Usage: %s [-p port] [-d] [-l logfile]\n", argv[0]);
562 printf(
"\t\t-p port\t\tWhich port to listen to.\n");
563 printf(
"\t\t-d\t\tEnables daemon mode.\n");
564 printf(
"\t\t-l logfile\tWhich file to store the log to.\n");
571 printf(
"Settings:\n");
572 printf(
"Port:\t\t\t%i\n", webPort);
573 printf(
"Server root:\t\t%s\n", output_dir.
Data());
574 printf(
"Logfile:\t\t%s\n", log_file.
Data());
575 printf(
"daemon:\t\t\t%i\n",
daemon);
580 start(webPort, output_dir);
void memset(T *dest, T i, size_t num)
Copy to memory block [@dest, @dest+@num] num number of times the value of i of type @T with size @typ...
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
R__EXTERN TEveManager * gEve
R__EXTERN TSystem * gSystem
static MpdEventManager * Instance()
static int scan(char *input, char *output, size_t start, size_t max)
static int start(int webPort, TString output_dir)
static void sendFile(FILE *fp, int connecting_socket)
virtual InitStatus Init()
static int handleHttpGET(char *input, TString output_dir, int connecting_socket)
static int checkMime(char *extension, char *mime_type)
static int getRequestType(char *input)
static int GetExtension(char *input, char *output, size_t max)
static int start_server(void *ptr)
virtual ~MpdWebScreenshots()
static int getHttpVersion(char *input, char *output)
static int receive(int connecting_socket, TString output_dir)
virtual void Exec(Option_t *option)
virtual void SetParContainers()
static void sendHeader(const char *Status_code, char *Content_Type, int TotalSize, int socket)
static int acceptConnection(int currentSocket, TString output_dir)
static int sendString(const char *message, int socket)
const char * Data() const
static TString Format(const char *fmt,...)
Int_t Run(void *arg=nullptr, const int affinity=-1)
void(* VoidFunc_t)(void *)