00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef _ABYSS_H_
00036 #define _ABYSS_H_
00037
00038
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #ifdef ABYSS_WIN32
00061 #define DEFAULT_ROOT "c:\\abyss"
00062 #define DEFAULT_DOCS DEFAULT_ROOT"\\htdocs"
00063 #define DEFAULT_CONF_FILE DEFAULT_ROOT"\\conf\\abyss.conf"
00064 #define DEFAULT_LOG_FILE DEFAULT_ROOT"\\log\\abyss.log"
00065 #else
00066 #ifdef __rtems__
00067 #define DEFAULT_ROOT "/abyss"
00068 #else
00069 #define DEFAULT_ROOT "/usr/local/abyss"
00070 #endif
00071 #define DEFAULT_DOCS DEFAULT_ROOT"/htdocs"
00072 #define DEFAULT_CONF_FILE DEFAULT_ROOT"/conf/abyss.conf"
00073 #define DEFAULT_LOG_FILE DEFAULT_ROOT"/log/abyss.log"
00074 #endif
00075
00076
00077
00078
00079
00080 #define MAX_CONN 16
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 #define SERVER_VERSION "0.3"
00091 #define SERVER_HVERSION "ABYSS/0.3"
00092 #define SERVER_HTML_INFO "<p><HR><b><i><a href=\"http:\057\057abyss.linuxave.net\">ABYSS Web Server</a></i></b> version "SERVER_VERSION"<br>© <a href=\"mailto:mmoez@bigfoot.com\">Moez Mahfoudh</a> - 2000</p>"
00093 #define SERVER_PLAIN_INFO CRLF"--------------------------------------------------------------------------------"\
00094 CRLF"ABYSS Web Server version "SERVER_VERSION CRLF"(C) Moez Mahfoudh - 2000"
00095
00096
00097
00098
00099
00100 #ifdef ABYSS_WIN32
00101 #define strcasecmp(a,b) stricmp((a),(b))
00102 #else
00103 #define ioctlsocket(a,b,c) ioctl((a),(b),(c))
00104 #endif
00105
00106 #ifndef NULL
00107 #define NULL ((void *)0)
00108 #endif
00109
00110 #ifndef TRUE
00111 #define TRUE 1
00112 #endif
00113
00114 #ifndef FALSE
00115 #define FALSE 0
00116 #endif
00117
00118 #ifdef ABYSS_WIN32
00119 #define LBR "\n"
00120 #else
00121 #define LBR "\n"
00122 #endif
00123
00124
00125
00126
00127
00128 typedef unsigned long uint64;
00129 typedef long int64;
00130
00131 typedef unsigned int uint32;
00132 typedef int int32;
00133
00134 typedef unsigned short uint16;
00135 typedef short int16;
00136
00137 typedef unsigned char byte;
00138 typedef unsigned char uint8;
00139 typedef char int8;
00140
00141 #ifndef __cplusplus
00142 typedef int bool;
00143 #endif
00144
00145
00146
00147
00148
00149 typedef struct
00150 {
00151 void *data;
00152 uint32 size;
00153 uint32 staticid;
00154 } TBuffer;
00155
00156 bool BufferAlloc(TBuffer *buf,uint32 memsize);
00157 bool BufferRealloc(TBuffer *buf,uint32 memsize);
00158 void BufferFree(TBuffer *buf);
00159
00160
00161
00162
00163
00164
00165 typedef struct
00166 {
00167 TBuffer buffer;
00168 uint32 size;
00169 } TString;
00170
00171 bool StringAlloc(TString *s);
00172 bool StringConcat(TString *s,char *s2);
00173 bool StringBlockConcat(TString *s,char *s2,char **ref);
00174 void StringFree(TString *s);
00175 char *StringData(TString *s);
00176
00177
00178
00179
00180
00181
00182 typedef struct
00183 {
00184 void **item;
00185 uint16 size,maxsize;
00186 bool autofree;
00187 } TList;
00188
00189 void ListInit(TList *sl);
00190 void ListInitAutoFree(TList *sl);
00191 void ListFree(TList *sl);
00192 bool ListAdd(TList *sl,void *str);
00193 bool ListAddFromString(TList *list,char *c);
00194 bool ListFindString(TList *sl,char *str,uint16 *index);
00195
00196
00197
00198
00199
00200
00201 typedef struct
00202 {
00203 char *name,*value;
00204 uint16 hash;
00205 } TTableItem;
00206
00207 typedef struct
00208 {
00209 TTableItem *item;
00210 uint16 size,maxsize;
00211 } TTable;
00212
00213 void TableInit(TTable *t);
00214 void TableFree(TTable *t);
00215 bool TableAdd(TTable *t,char *name,char *value);
00216 bool TableAddReplace(TTable *t,char *name,char *value);
00217 bool TableFindIndex(TTable *t,char *name,uint16 *index);
00218 char *TableFind(TTable *t,char *name);
00219
00220
00221
00222
00223
00224
00225 #ifdef ABYSS_WIN32
00226 #include <windows.h>
00227 #define THREAD_ENTRYTYPE WINAPI
00228 #else
00229 #define THREAD_ENTRYTYPE
00230 #include <pthread.h>
00231 #endif
00232
00233 typedef uint32 (THREAD_ENTRYTYPE *TThreadProc)(void *);
00234 #ifdef ABYSS_WIN32
00235 typedef HANDLE TThread;
00236 #else
00237 typedef pthread_t TThread;
00238 typedef void* (*PTHREAD_START_ROUTINE)(void *);
00239 #endif
00240
00241 bool ThreadCreate(TThread *t,TThreadProc func,void *arg);
00242 bool ThreadRun(TThread *t);
00243 bool ThreadStop(TThread *t);
00244 bool ThreadKill(TThread *t);
00245 void ThreadWait(uint32 ms);
00246 void ThreadExit( TThread *t, int ret_value );
00247 void ThreadClose( TThread *t );
00248
00249
00250
00251
00252
00253 #ifdef ABYSS_WIN32
00254 typedef HANDLE TMutex;
00255 #else
00256 typedef pthread_mutex_t TMutex;
00257 #endif
00258
00259 bool MutexCreate(TMutex *m);
00260 bool MutexLock(TMutex *m);
00261 bool MutexUnlock(TMutex *m);
00262 bool MutexTryLock(TMutex *m);
00263 void MutexFree(TMutex *m);
00264
00265
00266
00267
00268
00269
00270 typedef struct _TPoolZone
00271 {
00272 char *pos,*maxpos;
00273 struct _TPoolZone *next,*prev;
00274
00275 char data[1];
00276 } TPoolZone;
00277
00278 typedef struct
00279 {
00280 TPoolZone *firstzone,*currentzone;
00281 uint32 zonesize;
00282 TMutex mutex;
00283 } TPool;
00284
00285 bool PoolCreate(TPool *p,uint32 zonesize);
00286 void PoolFree(TPool *p);
00287
00288 void *PoolAlloc(TPool *p,uint32 size);
00289 char *PoolStrdup(TPool *p,char *s);
00290
00291
00292
00293
00294
00295
00296 #ifdef ABYSS_WIN32
00297 #include <winsock.h>
00298 #else
00299 #include <sys/types.h>
00300 #include <sys/ioctl.h>
00301 #include <sys/socket.h>
00302 #include <sys/time.h>
00303 #include <netinet/in.h>
00304 #include <netinet/tcp.h>
00305 #include <netdb.h>
00306 #include <arpa/inet.h>
00307 #include <errno.h>
00308 #include <unistd.h>
00309
00310 #ifdef HAVE_SYS_FILIO_H
00311 #include <sys/filio.h>
00312 #endif
00313 #ifdef HAVE_SYS_IOCTL_H
00314 #include <sys/ioctl.h>
00315 #endif
00316
00317 #endif
00318
00319 #define TIME_INFINITE 0xffffffff
00320
00321 #ifdef ABYSS_WIN32
00322 typedef SOCKET TSocket;
00323 #else
00324 typedef uint32 TSocket;
00325 #endif
00326
00327 typedef struct in_addr TIPAddr;
00328
00329 #define IPB1(x) (((unsigned char *)(&x))[0])
00330 #define IPB2(x) (((unsigned char *)(&x))[1])
00331 #define IPB3(x) (((unsigned char *)(&x))[2])
00332 #define IPB4(x) (((unsigned char *)(&x))[3])
00333
00334 bool SocketInit();
00335
00336 bool SocketCreate(TSocket *s);
00337 bool SocketClose(TSocket *s);
00338
00339 uint32 SocketWrite(TSocket *s, char *buffer, uint32 len);
00340 uint32 SocketRead(TSocket *s, char *buffer, uint32 len);
00341 uint32 SocketPeek(TSocket *s, char *buffer, uint32 len);
00342
00343 bool SocketConnect(TSocket *s, TIPAddr *addr, uint16 port);
00344 bool SocketBind(TSocket *s, TIPAddr *addr, uint16 port);
00345
00346 bool SocketListen(TSocket *s, uint32 backlog);
00347 bool SocketAccept(TSocket *s, TSocket *ns,TIPAddr *ip);
00348
00349 uint32 SocketError();
00350
00351 uint32 SocketWait(TSocket *s,bool rd,bool wr,uint32 timems);
00352
00353 bool SocketBlocking(TSocket *s, bool b);
00354 uint32 SocketAvailableReadBytes(TSocket *s);
00355
00356
00357
00358
00359
00360
00361 #include <fcntl.h>
00362 #include <sys/types.h>
00363 #include <sys/stat.h>
00364 #include <limits.h>
00365
00366 #ifndef NAME_MAX
00367 #define NAME_MAX 1024
00368 #endif
00369
00370 #ifdef ABYSS_WIN32
00371 #ifndef __BORLANDC__
00372 #define O_APPEND _O_APPEND
00373 #define O_CREAT _O_CREAT
00374 #define O_EXCL _O_EXCL
00375 #define O_RDONLY _O_RDONLY
00376 #define O_RDWR _O_RDWR
00377 #define O_TRUNC _O_TRUNC
00378 #define O_WRONLY _O_WRONLY
00379 #define O_TEXT _O_TEXT
00380 #define O_BINARY _O_BINARY
00381 #endif
00382
00383 #define A_HIDDEN _A_HIDDEN
00384 #define A_NORMAL _A_NORMAL
00385 #define A_RDONLY _A_RDONLY
00386 #define A_SUBDIR _A_SUBDIR
00387 #else
00388 #define A_SUBDIR 1
00389 #define O_BINARY 0
00390 #define O_TEXT 0
00391 #endif
00392
00393 #ifdef ABYSS_WIN32
00394
00395 #ifndef __BORLANDC__
00396 typedef struct _stati64 TFileStat;
00397 typedef struct _finddata_t TFileInfo;
00398 typedef long TFileFind;
00399
00400 #else
00401
00402 typedef struct stat TFileStat;
00403 typedef struct finddata_t
00404 {
00405 char name[NAME_MAX+1];
00406 int attrib;
00407 uint64 size;
00408 time_t time_write;
00409 WIN32_FIND_DATA data;
00410 } TFileInfo;
00411 typedef HANDLE TFileFind;
00412 #endif
00413
00414 #else
00415
00416 #include <unistd.h>
00417 #include <dirent.h>
00418
00419 typedef struct stat TFileStat;
00420
00421 typedef struct finddata_t
00422 {
00423 char name[NAME_MAX+1];
00424 int attrib;
00425 uint64 size;
00426 time_t time_write;
00427 } TFileInfo;
00428
00429 typedef struct
00430 {
00431 char path[NAME_MAX+1];
00432 DIR *handle;
00433 } TFileFind;
00434
00435 #endif
00436
00437 typedef int TFile;
00438
00439 bool FileOpen(TFile *f, char *name, uint32 attrib);
00440 bool FileOpenCreate(TFile *f, char *name, uint32 attrib);
00441 bool FileClose(TFile *f);
00442
00443 bool FileWrite(TFile *f, void *buffer, uint32 len);
00444 int32 FileRead(TFile *f, void *buffer, uint32 len);
00445
00446 bool FileSeek(TFile *f, uint64 pos, uint32 attrib);
00447 uint64 FileSize(TFile *f);
00448
00449 bool FileStat(char *filename,TFileStat *filestat);
00450
00451 bool FileFindFirst(TFileFind *filefind,char *path,TFileInfo *fileinfo);
00452 bool FileFindNext(TFileFind *filefind,TFileInfo *fileinfo);
00453 void FileFindClose(TFileFind *filefind);
00454
00455
00456
00457
00458
00459 typedef struct
00460 {
00461 TSocket listensock;
00462 TFile logfile;
00463 TMutex logmutex;
00464 char *name;
00465 char *filespath;
00466 uint16 port;
00467 uint32 keepalivetimeout,keepalivemaxconn,timeout;
00468 TList handlers;
00469 TList defaultfilenames;
00470 void *defaulthandler;
00471 bool advertise;
00472 #ifdef _UNIX
00473 uid_t uid;
00474 gid_t gid;
00475 TFile pidfile;
00476 #endif
00477 } TServer;
00478
00479
00480
00481
00482
00483
00484 #define BUFFER_SIZE 4096
00485
00486 typedef struct _TConn
00487 {
00488 TServer *server;
00489 uint32 buffersize,bufferpos;
00490 uint32 inbytes,outbytes;
00491 TSocket socket;
00492 TIPAddr peerip;
00493 TThread thread;
00494 bool connected;
00495 bool inUse;
00496 void (*job)(struct _TConn *);
00497 char buffer[BUFFER_SIZE];
00498 } TConn;
00499
00500 TConn *ConnAlloc();
00501 void ConnFree(TConn *c);
00502
00503 bool ConnCreate(TConn *c, TSocket *s, void (*func)(TConn *));
00504 bool ConnProcess(TConn *c);
00505 bool ConnKill(TConn *c);
00506
00507 bool ConnWrite(TConn *c,void *buffer,uint32 size);
00508 bool ConnRead(TConn *c, uint32 timems);
00509 void ConnReadInit(TConn *c);
00510 bool ConnReadLine(TConn *c,char **z,uint32 timems);
00511
00512 bool ConnWriteFromFile(TConn *c,TFile *file,uint64 start,uint64 end,
00513 void *buffer,uint32 buffersize,uint32 rate);
00514
00515
00516
00517
00518
00519
00520 bool RangeDecode(char *str,uint64 filesize,uint64 *start,uint64 *end);
00521
00522
00523
00524
00525
00526 #include <time.h>
00527
00528 typedef struct tm TDate;
00529
00530 bool DateToString(TDate *tm,char *s);
00531 bool DateToLogString(TDate *tm,char *s);
00532
00533 bool DateDecode(char *s,TDate *tm);
00534
00535 int32 DateCompare(TDate *d1,TDate *d2);
00536
00537 bool DateFromGMT(TDate *d,time_t t);
00538 bool DateFromLocal(TDate *d,time_t t);
00539
00540 bool DateInit();
00541
00542
00543
00544
00545
00546 void Base64Encode(char *s,char *d);
00547
00548
00549
00550
00551
00552 typedef enum
00553 {
00554 m_unknown,m_get,m_put,m_head,m_post,m_delete,m_trace,m_options
00555 } TMethod;
00556
00557 typedef struct
00558 {
00559 TMethod method;
00560 uint32 nbfileds;
00561 char *uri;
00562 char *query;
00563 char *host;
00564 char *from;
00565 char *useragent;
00566 char *referer;
00567 char *requestline;
00568 char *user;
00569 uint16 port;
00570 TList cookies;
00571 TList ranges;
00572
00573 uint16 status;
00574 TString header;
00575
00576 bool keepalive,cankeepalive;
00577 bool done;
00578
00579 TServer *server;
00580 TConn *conn;
00581
00582 uint8 versionminor,versionmajor;
00583
00584 TTable request_headers,response_headers;
00585
00586 TDate date;
00587
00588 bool chunkedwrite,chunkedwritemode;
00589 } TSession;
00590
00591
00592
00593
00594
00595 #define CR '\r'
00596 #define LF '\n'
00597 #define CRLF "\r\n"
00598
00599 bool RequestValidURI(TSession *r);
00600 bool RequestValidURIPath(TSession *r);
00601 bool RequestUnescapeURI(TSession *r);
00602
00603 char *RequestHeaderValue(TSession *r,char *name);
00604
00605 bool RequestRead(TSession *r);
00606 void RequestInit(TSession *r,TConn *c);
00607 void RequestFree(TSession *r);
00608
00609 bool RequestAuth(TSession *r,char *credential,char *user,char *pass);
00610
00611
00612
00613
00614
00615 bool ResponseAddField(TSession *r,char *name,char *value);
00616 void ResponseWrite(TSession *r);
00617
00618 bool ResponseChunked(TSession *s);
00619
00620 void ResponseStatus(TSession *r,uint16 code);
00621 void ResponseStatusErrno(TSession *r);
00622
00623 bool ResponseContentType(TSession *r,char *type);
00624 bool ResponseContentLength(TSession *r,uint64 len);
00625
00626 void ResponseError(TSession *r);
00627
00628
00629
00630
00631
00632
00633 char *HTTPReasonByStatus(uint16 status);
00634
00635 int32 HTTPRead(TSession *s,char *buffer,uint32 len);
00636
00637 bool HTTPWrite(TSession *s,char *buffer,uint32 len);
00638 bool HTTPWriteEnd(TSession *s);
00639
00640
00641
00642
00643
00644 typedef bool (*URIHandler) (TSession *);
00645
00646 bool ServerCreate(TServer *srv,char *name,uint16 port,char *filespath,
00647 char *logfilename);
00648 void ServerFree(TServer *srv);
00649
00650 void ServerInit(TServer *srv);
00651 void ServerRun(TServer *srv);
00652 void ServerRunOnce(TServer *srv);
00653
00654 bool ServerAddHandler(TServer *srv,URIHandler handler);
00655 void ServerDefaultHandler(TServer *srv,URIHandler handler);
00656
00657 bool LogOpen(TServer *srv, char *filename);
00658 void LogWrite(TServer *srv,char *c);
00659 void LogClose(TServer *srv);
00660
00661
00662
00663
00664
00665
00666 void MIMETypeInit();
00667 bool MIMETypeAdd(char *type,char *ext);
00668 char *MIMETypeFromExt(char *ext);
00669 char *MIMETypeFromFileName(char *filename);
00670 char *MIMETypeGuessFromFile(char *filename);
00671
00672
00673
00674
00675
00676
00677 bool ConfReadMIMETypes(char *filename);
00678 bool ConfReadServerFile(char *filename,TServer *srv);
00679
00680
00681
00682
00683
00684
00685 void TraceMsg(char *fmt,...);
00686 void TraceExit(char *fmt,...);
00687
00688
00689
00690
00691
00692
00693 bool SessionLog(TSession *s);
00694
00695
00696 #ifdef __cplusplus
00697 }
00698 #endif
00699
00700 #endif