HttpServer.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: HttpServer.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 /*********************************************************************** 00045 * Author: Klaus Schoeffmann * 00046 * original code by Michael Kropfberger (from Mohican :) * 00047 ************************************************************************/ 00048 00049 00050 #ifndef __HttpServer_hpp__ 00051 #define __HttpServer_hpp__ 00052 00053 #ifndef WIN32 00054 #include <stdio.h> 00055 #include <sys/time.h> 00056 #include <time.h> 00057 #include <string.h> 00058 #include <linux/types.h> 00059 #include <netinet/in.h> 00060 #include <arpa/inet.h> 00061 #include <netdb.h> 00062 #include <strings.h> 00063 #include <unistd.h> 00064 #include <sys/socket.h> 00065 #include <sys/types.h> 00066 #include <sys/wait.h> 00067 #include <stdlib.h> 00068 #include <errno.h> 00069 #include <unistd.h> 00070 #include <fcntl.h> 00071 #endif 00072 00073 #include "VThread.hpp" 00074 00075 00076 #define METHOD_GET 0 00077 #define METHOD_POST 1 00078 00079 #define MAX_LEN 4096 00080 #define VERSION "HTTP/1.0" 00081 #define OK "200 OK" 00082 #define NO_CONTENT "204 No Content" 00083 #define NOT_FOUND "404 Not Found" 00084 #define NOT_IMPL "501 Not Implemented" 00085 00086 #define DEFAULT_PAGE "index.html" 00087 #define ERR404_PAGE "404.html" 00088 00089 00090 typedef enum {mime_unknown=0,mime_html,mime_plain,mime_gif,mime_jpg,mime_mp4,mime_vit,mime_ogg,mime_mkv} HttpMimeType; 00091 #define NUM_KNOWN_HTTP_MIMETYPES 9 00092 static char *HttpMimeTypeStrings[NUM_KNOWN_HTTP_MIMETYPES] = {"application/octet-stream","text/html","text/plain", 00093 "image/gif","image/jpg", 00094 "video/mpeg","video/x-vit","video/x-ogg","video/x-mkv"}; 00095 00096 00097 class HttpServer : public VThread { 00098 public: 00099 00100 00106 HttpServer(int port = 8080, const char *rootdir = ".", const char *cgidir = "/cgi-bin/"); 00108 ~HttpServer(); 00109 00110 00112 void run(); 00113 00115 void stop(); 00116 00117 private: 00119 int sock; 00121 int port; 00123 char *rootdir; 00125 char *cgidir; 00127 bool active; 00129 bool shouldBeActive; 00130 00135 int sendResource(int sock, char *uri); 00136 00144 int runCGI(int sock, char *uri, int method, char *body, int body_len); 00145 00150 char *findExtension(char *uri,char *ext); 00151 00152 }; 00153 00154 00155 #endif