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
113struct direct {
114 ino_t d_ino;
115 int d_reclen;
116 int d_namlen;
117 char d_name[MAXNAMLEN+1];
118};
119struct _dircontents {
120 char *_d_entry;
121 struct _dircontents *_d_next;
122};
123typedef struct _dirdesc {
124 int dd_id;
125 long dd_loc;
126 struct _dircontents *dd_contents;
127 struct _dircontents *dd_cp;
128} DIR;
129struct timezone {
130 int tz_minuteswest;
131 int tz_dsttime;
132};
133/* pthread_ typedefs */
134typedef HANDLE pthread_t;
135typedef struct thread_attr {
136 DWORD dwStackSize;
137 DWORD dwCreatingFlag;
138 int priority;
139} pthread_attr_t;
140typedef struct {
141 int dummy;
142} pthread_condattr_t;
143typedef unsigned int uint;
144typedef struct {
145 uint waiting;
146 HANDLE semaphore;
147} pthread_cond_t;
148typedef CRITICAL_SECTION pthread_mutex_t;
149#endif
150
151typedef 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;
161 short int server_maxconn;
162 short int server_maxidle;
163} CONFIG;
164typedef 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;
201typedef struct {
202 pthread_t handle;
203 unsigned long int id;
204#ifdef WIN32
205 SOCKET socket;
206#else
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;
215typedef 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
227HINSTANCE hInst;
228#endif
229typedef struct {
230 pthread_mutex_t Crypt;
231 pthread_mutex_t Global;
232 pthread_mutex_t SQL;
233} LOCK;
234extern LOCK Lock;
235extern char program_name[255];
236extern CONFIG config;
237extern CONNECTION *conn;
238
239/* function forwards */
240/* win32.c functions */
241#ifdef WIN32
242unsigned sleep(unsigned seconds);
243DIR *opendir(const char *);
244void closedir(DIR *);
245#define rewinddir(dirp) seekdir(dirp, 0L)
246void seekdir(DIR *, long);
247long telldir(DIR *);
248struct direct *readdir(DIR *);
249int gettimeofday(struct timeval *tv, struct timezone *tz);
250/* registry and service control functions */
251int get_reg_entries(void);
252/* The following is to emulate the posix thread interface */
253#define pthread_mutex_init(A,B) InitializeCriticalSection(A)
254#define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
255#define pthread_mutex_unlock(A) LeaveCriticalSection(A)
256#define pthread_mutex_destroy(A) DeleteCriticalSection(A)
257#define pthread_handler_decl(A,B) unsigned __cdecl A(void *B)
258#define pthread_self() GetCurrentThreadId()
259typedef unsigned (__cdecl *pthread_handler)(void *);
260int pthread_attr_init(pthread_attr_t *connect_att);
261int pthread_attr_setstacksize(pthread_attr_t *connect_att, DWORD stack);
262int pthread_attr_setprio(pthread_attr_t *connect_att, int priority);
263int pthread_attr_destroy(pthread_attr_t *connect_att);
264int pthread_create(pthread_t *thread_id, pthread_attr_t *attr, unsigned (__stdcall *func)( void * ), void *param);
265void pthread_exit(unsigned A);
266#endif
267/* main.c functions */
268void dorequest(int sid);
269/* config.c functions */
270int config_read(void);
271/* format.c */
272void decodeurl(char *pEncoded);
273void fixslashes(char *pOriginal);
274int hex2int(char *pChars);
275void striprn(char *string);
276void swapchar(char *string, char oldchar, char newchar);
277char *nullhttpd_strcasestr(const char *src, const char *query);
278char *strcatf(char *dest, const char *format, ...);
279int printhex(const char *format, ...);
280int printht(const char *format, ...);
281/* http.c functions */
282void printerror(int sid, int status, char* title, char* text);
283char *get_mime_type(char *name);
284void ReadPOSTData(int sid);
285int read_header(int sid);
286void send_header(int sid, int cacheable, int status, char *title, char *extra_header, char *mime_type, int length, time_t mod);
287void send_fileheader(int sid, int cacheable, int status, char *title, char *extra_header, char *mime_type, int length, time_t mod);
288/* server.c functions */
289void logaccess(int loglevel, const char *format, ...);
290void logerror(const char *format, ...);
291int nullhttpd_getsid(void);
292void flushbuffer(int sid);
293int prints(const char *format, ...);
294int sgets(char *buffer, int max, int fd);
295int closeconnect(int sid, int exitflag);
296void server_shutdown();
297int sockinit(void);
298void cgiinit(void);
299NULLHTTPD_SHARE void init(void);
300#ifdef WIN32
301NULLHTTPD_SHARE void WSAReaper(void *x);
302#endif
303NULLHTTPD_SHARE void accept_loop(void *x);
#define max(x, y)
Definition mar3xx_pck.c:32
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
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 * get_mime_type(char *name)
Definition nullhttpd_http.c:37
char * strcatf(char *dest, const char *format,...)
Definition nullhttpd_format.c:123
CONNECTION * conn
Definition nullhttpd_extern.c:4
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_extern.c:6
#define MAX_REPLYSIZE
Definition nullhttpd.h:91
NULLHTTPD_SHARE void accept_loop(void *x)
Definition nullhttpd_server.c:595
LOCK Lock
Definition nullhttpd_extern.c:5
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_extern.c:3
int closeconnect(int sid, int exitflag)
Definition nullhttpd_server.c:246
void printerror(int sid, int status, char *title, char *text)
Definition nullhttpd_http.c:23
void ReadPOSTData(int sid)
Definition nullhttpd_http.c:91
char * nullhttpd_strcasestr(const char *src, const char *query)
Definition nullhttpd_format.c:95
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
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
int socket
Definition nullhttpd.h:207
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:229
pthread_mutex_t Crypt
Definition nullhttpd.h:230
pthread_mutex_t SQL
Definition nullhttpd.h:232
pthread_mutex_t Global
Definition nullhttpd.h:231
Definition nullhttpd.h:215
int in
Definition nullhttpd.h:220
int out
Definition nullhttpd.h:221
Definition restApi.cpp:78