areaDetector  3-14
EPICS areaDetector framework
nullhttpd.h
Go to the documentation of this file.
1 /*
2  Null httpd -- simple http server
3  Copyright (C) 2001-2002 Dan Cahill
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /* #includes */
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <sys/stat.h>
28 #ifdef WIN32
29 // #pragma comment(lib, "libcmt.lib")
30  #pragma comment(lib, "ws2_32.lib")
31 // #define _MT 1
32  #define WIN32_LEAN_AND_MEAN
33  #include <windows.h>
34  #include <winsock2.h>
35  #include <mstcpip.h>
36  #include <ws2tcpip.h>
37  #include <process.h>
38  #include <shellapi.h>
39  #include <signal.h>
40  #include <windowsx.h>
41  #include <io.h>
42  #include <direct.h>
43  #define snprintf _snprintf
44  #define vsnprintf _vsnprintf
45  #define strcasecmp stricmp
46  #define strncasecmp strnicmp
47  /* these are to avoid a clash with opendir() etc defined elsewhere */
48  #define opendir nullhttpd_win32_opendir
49  #define closedir nullhttpd_win32_closedir
50  #define readdir nullhttpd_win32_readdir
51  #define seekdir nullhttpd_win32_seekdir
52  #define gettimeofday nullhttpd_win32_gettimeofday
53  #define sleep nullhttpd_win32_sleep
54 #else
55  #include <dirent.h>
56  #include <netdb.h>
57  #include <paths.h>
58  #include <pthread.h>
59  #include <signal.h>
60  #include <unistd.h>
61  #include <arpa/inet.h>
62  #include <netinet/in.h>
63  #include <sys/resource.h>
64  #include <sys/socket.h>
65  #include <sys/time.h>
66  #include <sys/types.h>
67  #include <sys/wait.h>
68 #endif
69 
70 #ifdef NULLHTTPD_DYNAMIC
71 #ifdef NULLHTTPD_EXPORTS
72 #include <epicsExport.h>
73 #else
74 #include <shareLib.h>
75 #endif
76 #define NULLHTTPD_SHARE epicsShareExtern
77 #else
78 #define NULLHTTPD_SHARE
79 #endif
80 
81 /* #defines and typedefs */
82 #ifdef WIN32
83 #define APPTITLE "Null httpd"
84 #define DEFAULT_BASE_DIR "C:\\httpd"
85 #else
86 #define DEFAULT_BASE_DIR "/usr/local/httpd"
87 #endif
88 #define SERVER_NAME "Null httpd 0.5.1"
89 
90 #define MAX_POSTSIZE 33554432 /* arbitrary 32 MB limit for POST request sizes */
91 #define MAX_REPLYSIZE 65536 /* arbitrary 64 KB limit for reply buffering */
92 
93 #ifdef WIN32
94 #define S_IFMT _S_IFMT
95 #define S_IFDIR _S_IFDIR
96 #define S_IFCHR _S_IFCHR
97 #define S_IFIFO _S_IFIFO
98 #define S_IFREG _S_IFREG
99 #define S_IREAD _S_IREAD
100 #define S_IWRITE _S_IWRITE
101 #define S_IEXEC _S_IEXEC
102 #define S_IFLNK 0000000
103 #define S_IFSOCK 0000000
104 #define S_IFBLK 0000000
105 #define S_IROTH 0000004
106 #define S_IWOTH 0000002
107 #define S_IXOTH 0000001
108 #define S_ISDIR(x) (x & _S_IFDIR)
109 #define S_ISLNK(x) 0
110 #define ATTRIBUTES (_A_RDONLY|_A_HIDDEN|_A_SYSTEM|_A_SUBDIR)
111 #define MAXNAMLEN 255
112 
113 struct direct {
114  ino_t d_ino;
115  int d_reclen;
116  int d_namlen;
117  char d_name[MAXNAMLEN+1];
118 };
119 struct _dircontents {
120  char *_d_entry;
121  struct _dircontents *_d_next;
122 };
123 typedef struct _dirdesc {
124  int dd_id;
125  long dd_loc;
126  struct _dircontents *dd_contents;
127  struct _dircontents *dd_cp;
128 } DIR;
129 struct timezone {
130  int tz_minuteswest;
131  int tz_dsttime;
132 };
133 /* pthread_ typedefs */
134 typedef HANDLE pthread_t;
135 typedef struct thread_attr {
136  DWORD dwStackSize;
137  DWORD dwCreatingFlag;
138  int priority;
139 } pthread_attr_t;
140 typedef struct {
141  int dummy;
142 } pthread_condattr_t;
143 typedef unsigned int uint;
144 typedef struct {
145  uint waiting;
146  HANDLE semaphore;
147 } pthread_cond_t;
148 typedef CRITICAL_SECTION pthread_mutex_t;
149 #endif
150 
151 typedef struct {
152  char config_filename[255];
153  char server_base_dir[255];
154  char server_bin_dir[255];
155  char server_cgi_dir[255];
156  char server_etc_dir[255];
157  char server_htdocs_dir[255];
158  char server_hostname[64];
159  short int server_port;
160  short int server_loglevel;
161  short int server_maxconn;
162  short int server_maxidle;
163 } CONFIG;
164 typedef struct {
165  // incoming data
166  char in_Connection[16];
168  char in_ContentType[128];
169  char in_Cookie[1024];
170  char in_Host[64];
171  char in_IfModifiedSince[64];
172  char in_PathInfo[128];
173  char in_Protocol[16];
174  char in_QueryString[1024];
175  char in_Referer[128];
176  char in_RemoteAddr[16];
178  char in_RequestMethod[8];
179  char in_RequestURI[1024];
180  char in_ScriptName[128];
181  char in_UserAgent[128];
182  // outgoing data
183  short int out_status;
184  char out_CacheControl[16];
185  char out_Connection[16];
187  char out_Date[64];
188  char out_Expires[64];
189  char out_LastModified[64];
190  char out_Pragma[16];
191  char out_Protocol[16];
192  char out_Server[128];
193  char out_ContentType[128];
194  char out_ReplyData[MAX_REPLYSIZE];
195  short int out_headdone;
196  short int out_bodydone;
197  short int out_flushed;
198  // user data
199  char envbuf[8192];
200 } CONNDATA;
201 typedef struct {
202  pthread_t handle;
203  unsigned long int id;
204 #ifdef WIN32
205  SOCKET socket;
206 #else
207  int socket;
208 #endif
209  struct sockaddr_in ClientAddr;
210  time_t ctime; // Creation time
211  time_t atime; // Last Access time
212  char *PostData;
214 } CONNECTION;
215 typedef struct {
216 #ifdef WIN32
217  HANDLE in;
218  HANDLE out;
219 #else
220  int in; /* so large enough to take a HANDLE on WIN32 64bit */
221  int out;
222 #endif
223 } pipe_fd;
224 
225 /* global vars */
226 #ifdef WIN32
227 HINSTANCE hInst;
228 #endif
229 struct {
230  pthread_mutex_t Crypt;
231  pthread_mutex_t Global;
232  pthread_mutex_t SQL;
234 char program_name[255];
237 
238 /* function forwards */
239 /* win32.c functions */
240 #ifdef WIN32
241 unsigned sleep(unsigned seconds);
242 DIR *opendir(const char *);
243 void closedir(DIR *);
244 #define rewinddir(dirp) seekdir(dirp, 0L)
245 void seekdir(DIR *, long);
246 long telldir(DIR *);
247 struct direct *readdir(DIR *);
248 int gettimeofday(struct timeval *tv, struct timezone *tz);
249 /* registry and service control functions */
250 int get_reg_entries(void);
251 /* The following is to emulate the posix thread interface */
252 #define pthread_mutex_init(A,B) InitializeCriticalSection(A)
253 #define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
254 #define pthread_mutex_unlock(A) LeaveCriticalSection(A)
255 #define pthread_mutex_destroy(A) DeleteCriticalSection(A)
256 #define pthread_handler_decl(A,B) unsigned __cdecl A(void *B)
257 #define pthread_self() GetCurrentThreadId()
258 typedef unsigned (__cdecl *pthread_handler)(void *);
259 int pthread_attr_init(pthread_attr_t *connect_att);
260 int pthread_attr_setstacksize(pthread_attr_t *connect_att, DWORD stack);
261 int pthread_attr_setprio(pthread_attr_t *connect_att, int priority);
262 int pthread_attr_destroy(pthread_attr_t *connect_att);
263 int pthread_create(pthread_t *thread_id, pthread_attr_t *attr, unsigned (__stdcall *func)( void * ), void *param);
264 void pthread_exit(unsigned A);
265 #endif
266 /* main.c functions */
267 void dorequest(int sid);
268 /* config.c functions */
269 int config_read(void);
270 /* format.c */
271 void decodeurl(char *pEncoded);
272 void fixslashes(char *pOriginal);
273 int hex2int(char *pChars);
274 void striprn(char *string);
275 void swapchar(char *string, char oldchar, char newchar);
276 char *nullhttpd_strcasestr(const char *src, const char *query);
277 char *strcatf(char *dest, const char *format, ...);
278 int printhex(const char *format, ...);
279 int printht(const char *format, ...);
280 /* http.c functions */
281 void printerror(int sid, int status, char* title, char* text);
282 char *get_mime_type(char *name);
283 void ReadPOSTData(int sid);
284 int read_header(int sid);
285 void send_header(int sid, int cacheable, int status, char *title, char *extra_header, char *mime_type, int length, time_t mod);
286 void send_fileheader(int sid, int cacheable, int status, char *title, char *extra_header, char *mime_type, int length, time_t mod);
287 /* server.c functions */
288 void logaccess(int loglevel, const char *format, ...);
289 void logerror(const char *format, ...);
290 int nullhttpd_getsid(void);
291 void flushbuffer(int sid);
292 int prints(const char *format, ...);
293 int sgets(char *buffer, int max, int fd);
294 int closeconnect(int sid, int exitflag);
295 void server_shutdown();
296 int sockinit(void);
297 void cgiinit(void);
298 NULLHTTPD_SHARE void init(void);
299 #ifdef WIN32
300 NULLHTTPD_SHARE void WSAReaper(void *x);
301 #endif
302 NULLHTTPD_SHARE void accept_loop(void *x);
#define max(x, y)
Definition: mar3xx_pck.c:32
int x
Definition: makeAdl.py:438
string text
Definition: makeAdl.py:440
name
Definition: makeDbAndEdl.py:232
void send_fileheader(int sid, int cacheable, int status, char *title, char *extra_header, char *mime_type, int length, time_t mod)
Definition: nullhttpd_http.c:235
void fixslashes(char *pOriginal)
Definition: nullhttpd_format.c:45
pthread_mutex_t Global
Definition: nullhttpd.h:231
void striprn(char *string)
Definition: nullhttpd_format.c:80
void server_shutdown()
Definition: nullhttpd_server.c:303
int read_header(int sid)
Definition: nullhttpd_http.c:119
int printhex(const char *format,...)
Definition: nullhttpd_format.c:136
void dorequest(int sid)
This is called whenever a client requests a stream.
Definition: ffmpegServer.cpp:39
char * nullhttpd_strcasestr(const char *src, const char *query)
Definition: nullhttpd_format.c:95
pthread_mutex_t SQL
Definition: nullhttpd.h:232
CONNECTION * conn
Definition: nullhttpd.h:236
int prints(const char *format,...)
Definition: nullhttpd_server.c:188
void swapchar(char *string, char oldchar, char newchar)
Definition: nullhttpd_format.c:87
int hex2int(char *pChars)
Definition: nullhttpd_format.c:54
void decodeurl(char *pEncoded)
Definition: nullhttpd_format.c:21
char program_name[255]
Definition: nullhttpd.h:234
pthread_mutex_t Crypt
Definition: nullhttpd.h:230
#define MAX_REPLYSIZE
Definition: nullhttpd.h:91
NULLHTTPD_SHARE void accept_loop(void *x)
Definition: nullhttpd_server.c:595
void cgiinit(void)
int sgets(char *buffer, int max, int fd)
Definition: nullhttpd_server.c:209
int config_read(void)
this dummy function is here to satisfy nullhttpd
Definition: ffmpegServer.cpp:160
void flushbuffer(int sid)
Definition: nullhttpd_server.c:170
int sockinit(void)
Definition: nullhttpd_server.c:385
CONFIG config
Definition: nullhttpd.h:235
int closeconnect(int sid, int exitflag)
Definition: nullhttpd_server.c:246
char * strcatf(char *dest, const char *format,...)
Definition: nullhttpd_format.c:123
void printerror(int sid, int status, char *title, char *text)
Definition: nullhttpd_http.c:23
char * get_mime_type(char *name)
Definition: nullhttpd_http.c:37
void ReadPOSTData(int sid)
Definition: nullhttpd_http.c:91
int nullhttpd_getsid(void)
Definition: nullhttpd_server.c:82
void send_header(int sid, int cacheable, int status, char *title, char *extra_header, char *mime_type, int length, time_t mod)
Definition: nullhttpd_http.c:200
#define NULLHTTPD_SHARE
Definition: nullhttpd.h:78
void logaccess(int loglevel, const char *format,...)
Definition: nullhttpd_server.c:29
void logerror(const char *format,...)
Definition: nullhttpd_server.c:56
int printht(const char *format,...)
Definition: nullhttpd_format.c:157
struct @8 Lock
NULLHTTPD_SHARE void init(void)
Definition: nullhttpd_server.c:487
#define DWORD
Definition: pco_structures.h:443
Definition: nullhttpd.h:151
short int server_loglevel
Definition: nullhttpd.h:160
short int server_maxconn
Definition: nullhttpd.h:161
short int server_port
Definition: nullhttpd.h:159
short int server_maxidle
Definition: nullhttpd.h:162
Definition: nullhttpd.h:164
int out_ContentLength
Definition: nullhttpd.h:186
short int out_bodydone
Definition: nullhttpd.h:196
int in_ContentLength
Definition: nullhttpd.h:167
short int out_status
Definition: nullhttpd.h:183
int in_RemotePort
Definition: nullhttpd.h:177
short int out_headdone
Definition: nullhttpd.h:195
short int out_flushed
Definition: nullhttpd.h:197
Definition: nullhttpd.h:201
time_t ctime
Definition: nullhttpd.h:210
unsigned long int id
Definition: nullhttpd.h:203
char * PostData
Definition: nullhttpd.h:212
time_t atime
Definition: nullhttpd.h:211
pthread_t handle
Definition: nullhttpd.h:202
CONNDATA * dat
Definition: nullhttpd.h:213
Definition: nullhttpd.h:215
int in
Definition: nullhttpd.h:220
int out
Definition: nullhttpd.h:221
Definition: restApi.cpp:78