6#include "TEveManager.h"
11#include <sys/socket.h>
13#include <netinet/in.h>
20 : FairTask(
"MpdWebScreenshots", 0)
29 : FairTask(name, iVerbose)
31 outputDir = output_dir;
39 if (strlen(outputDir) > 0)
42 if (stat(outputDir, &st) == -1) mkdir(outputDir, 0700);
44 cout<<
"Images of the event display will be saved in directory: "<<outputDir<<endl;
51 if (fVerbose > 1) cout<<
"MpdWebScreenshots::Init()"<<endl;
59 if (isWeb && (!isWebStarted))
69 TThread* threadWebServ =
new TThread((TThread::VoidFunc_t)&start_server, (
void*)pPar, TThread::kNormalPriority);
82 gSystem->ProcessEvents();
84 TString fileName = outputDir;
85 if (isMultiFiles == 1)
90 if ((iFormatFiles == 0) || (iFormatFiles == 2))
93 gEve->GetDefaultGLViewer()->SavePicture(fileName.Data());
99 gEve->GetDefaultGLViewer()->SavePicture(fileName.Data());
114int MpdWebScreenshots::daemonize()
140 if ((chdir(
"/")) < 0)
146int MpdWebScreenshots::sendString(
const char *message,
int socket)
148 return send(socket, message, strlen(message), 0);
151void MpdWebScreenshots::sendHeader(
const char* Status_code,
char* Content_Type,
int TotalSize,
int socket)
153 char* head = (
char*)
"\r\nHTTP/1.1 ";
154 char* content_head = (
char*)
"\r\nContent-Type: ";
155 char* server_head = (
char*)
"\r\nServer: PT06";
156 char* length_head = (
char*)
"\r\nContent-Length: ";
157 char* date_head = (
char*)
"\r\nDate: ";
158 char* newline = (
char*)
"\r\n";
163 char contentLength[100];
164 sprintf(contentLength,
"%i", TotalSize);
166 char* message = (
char*)malloc(
168 strlen(content_head) +
169 strlen(server_head) +
170 strlen(length_head) +
173 strlen(Status_code) +
174 strlen(Content_Type) +
175 strlen(contentLength) +
184 strcpy(message, head);
185 strcat(message, Status_code);
186 strcat(message, content_head);
187 strcat(message, Content_Type);
188 strcat(message, server_head);
189 strcat(message, length_head);
190 strcat(message, contentLength);
191 strcat(message, date_head);
192 strcat(message, (
char*)ctime(&rawtime));
193 strcat(message, newline);
195 sendString(message, socket);
202void MpdWebScreenshots::sendFile(FILE* fp,
int connecting_socket)
204 int current_char = 0;
207 current_char = fgetc(fp);
209 send(connecting_socket, ¤t_char,
sizeof(
char), 0);
211 while (current_char != EOF);
214int MpdWebScreenshots::scan(
char* input,
char* output,
size_t start,
size_t max)
216 if (start >= strlen(input))
219 int appending_char_count = 0;
223 for (;
i < strlen(input);
i++)
225 if (*(input +
i) !=
'\t' && *(input +
i) !=
' ' && *(input +
i) !=
'\n' && *(input +
i) !=
'\r')
229 *(output + appending_char_count) = *(input +
i ) ;
230 appending_char_count += 1;
237 *(output + appending_char_count) =
'\0';
242 for (;
i < strlen(input);
i++)
244 if (*(input +
i ) !=
'\t' && *(input +
i) !=
' ' && *(input +
i) !=
'\n' && *(input +
i) !=
'\r')
251int MpdWebScreenshots::checkMime(
char* extension,
char* mime_type)
253 char* current_word = (
char*) malloc(600);
254 char* word_holder = (
char*) malloc(600);
255 char* line = (
char*) malloc(200);
258 TString mime_file =
"mime.types";
259 FILE* mimeFile = fopen(mime_file.Data(),
"r");
262 mime_type = (
char*)malloc(200);
264 memset(mime_type,
'\0',200);
266 while (fgets(line, 200, mimeFile) != NULL)
270 startline =
scan(line, current_word, 0, 600);
273 startline =
scan(line, word_holder, startline, 600);
276 if (strcmp(word_holder, extension) == 0)
278 memcpy(mime_type, current_word, strlen(current_word));
300int MpdWebScreenshots::getHttpVersion(
char* input,
char* output)
302 char*
filename = (
char*) malloc(100);
303 int start =
scan(input, filename, 4, 100);
306 if (
scan(input, output, start, 20))
308 output[strlen(output)+1] =
'\0';
310 if (strcmp(
"HTTP/1.1" , output) == 0)
314 if (strcmp(
"HTTP/1.0", output) == 0)
327int MpdWebScreenshots::GetExtension(
char* input,
char* output,
size_t max)
330 int appended_position = 0;
332 size_t i = 0, count = 0;
333 for (;
i < strlen(input);
i++)
335 if (in_position == 1)
339 output[appended_position] = input[
i];
340 appended_position += 1;
348 output[appended_position+1] =
'\0';
350 if (strlen(output) > 0)
357int MpdWebScreenshots::handleHttpGET(
char* input, TString output_dir,
int connecting_socket)
359 char*
filename = (
char*) malloc(200 *
sizeof(
char));
360 char* path = (
char*) malloc(1000 *
sizeof(
char));
361 char* extension = (
char*) malloc(10 *
sizeof(
char));
362 char* mime = (
char*) malloc(200 *
sizeof(
char));
363 char* httpVersion = (
char*) malloc(20 *
sizeof(
char));
365 int contentLength = 0, mimeSupported = 0;
368 memset(filename,
'\0', 200);
369 memset(extension,
'\0', 10);
371 memset(httpVersion,
'\0', 20);
373 int fileNameLength =
scan(input, filename, 5, 200);
376 while (filename[
i]!=
'\0' && filename[
i]!=
'?')
379 if (filename[
i] ==
'?')
382 if (fileNameLength <= 0)
385 if (getHttpVersion(input, httpVersion) == -1)
387 sendString(
"501 Not Implemented\n", connecting_socket);
391 if (GetExtension(filename, extension, 10) == -1)
393 printf(
"File extension not existing");
394 sendString(
"400 Bad Request\n", connecting_socket);
403 mimeSupported = checkMime(extension, mime);
404 if (mimeSupported != 1)
406 printf(
"Mime not supported");
407 sendString(
"400 Bad Request\n", connecting_socket);
417 strcpy(path, output_dir.Data());
418 strcat(path, filename);
420 FILE* fp = fopen(path,
"rb");
423 printf(
"\nUnable to open file %s\n",path);
425 sendString(
"404 Not Found\n", connecting_socket);
435 fseek(fp, 0, SEEK_END);
436 contentLength = ftell(fp);
438 if (contentLength < 0)
440 printf(
"File size is zero");
451 sendHeader(
"200 OK", mime, contentLength, connecting_socket);
453 sendFile(fp, connecting_socket);
465int MpdWebScreenshots::getRequestType(
char* input)
468 if (strlen(input) > 0)
471 char* requestType = (
char*) malloc(5);
472 scan(input, requestType, 0, 5);
474 if (type == 1 && strcmp(
"GET", requestType) == 0)
type = 1;
475 else if (type == 1 && strcmp(
"HEAD", requestType) == 0)
type = 2;
476 else if (strlen(input) > 4 && strcmp(
"POST", requestType) == 0)
type = 0;
482int MpdWebScreenshots::receive(
int connecting_socket, TString output_dir)
487 if ((recv(connecting_socket, buffer,
BUFFER_SIZE, 0)) == -1)
489 printf(
"Error handling incoming request");
493 int request = getRequestType(buffer);
495 handleHttpGET(buffer, output_dir, connecting_socket);
500 sendString(
"501 Not Implemented\n", connecting_socket);
502 sendString(
"400 Bad Request\n", connecting_socket);
510int MpdWebScreenshots::acceptConnection(
int currentSocket, TString output_dir)
512 sockaddr_storage connectorSocket;
513 socklen_t addressSize =
sizeof(connectorSocket);
515 int connecting_socket = accept(currentSocket, (
struct sockaddr *)&(connectorSocket), &addressSize);
516 if (connecting_socket < 0)
518 perror(
"Accepting sockets");
527 if (receive(connecting_socket, output_dir) < 0)
533 close(connecting_socket);
535 while (-1 != waitpid(-1, NULL, WNOHANG));
540int MpdWebScreenshots::start(
int webPort, TString output_dir)
543 int currentSocket = socket(AF_INET, SOCK_STREAM, 0);
544 if (currentSocket == -1)
546 perror(
"Create socket");
551 struct sockaddr_in address;
552 address.sin_family = AF_INET;
553 address.sin_addr.s_addr = INADDR_ANY;
554 address.sin_port = htons(webPort);
556 if (bind(currentSocket, (
struct sockaddr *)&address,
sizeof(address)) < 0)
558 perror(
"Bind to port");
565 perror(
"Listen on port");
570 acceptConnection(currentSocket, output_dir);
573int MpdWebScreenshots::start_server(
void* ptr)
578 int daemon = pPar->
daemon;
584 for (
int parameterCount = 1; parameterCount < argc; parameterCount++)
587 if (strcmp(argv[parameterCount],
"-p") == 0)
591 printf(
"Setting port to %i\n", atoi(argv[parameterCount]));
592 webPort = atoi(argv[parameterCount]);
597 if (strcmp(argv[parameterCount],
"-d") == 0)
599 printf(
"Setting daemon = TRUE");
604 if (strcmp(argv[parameterCount],
"-l") == 0)
608 printf(
"Setting logfile = %s\n", argv[parameterCount]);
609 log_file = (TString) (
char*) argv[parameterCount];
613 printf(
"Usage: %s [-p port] [-d] [-l logfile]\n", argv[0]);
614 printf(
"\t\t-p port\t\tWhich port to listen to.\n");
615 printf(
"\t\t-d\t\tEnables daemon mode.\n");
616 printf(
"\t\t-l logfile\tWhich file to store the log to.\n");
623 printf(
"Settings:\n");
624 printf(
"Port:\t\t\t%i\n", webPort);
625 printf(
"Server root:\t\t%s\n", output_dir.Data());
626 printf(
"Logfile:\t\t%s\n", log_file.Data());
627 printf(
"daemon:\t\t\t%i\n", daemon);
632 start(webPort, output_dir);
void memset(T *dest, T i, size_t num)
uses binary expansion of copied volume for speed up
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
static MpdEventManager * Instance()
virtual Int_t GetCurrentEvent()
virtual InitStatus Init()
virtual ~MpdWebScreenshots()
MpdWebScreenshots(const char *name, char *output_dir, bool isWebServer=false, Int_t iVerbose=0)
virtual void Exec(Option_t *option)
virtual void SetParContainers()