📄 abyss.h
字号:
#ifndef NAME_MAX
#define NAME_MAX 1024
#endif
#ifdef ABYSS_WIN32
#ifndef __BORLANDC__
#define O_APPEND _O_APPEND
#define O_CREAT _O_CREAT
#define O_EXCL _O_EXCL
#define O_RDONLY _O_RDONLY
#define O_RDWR _O_RDWR
#define O_TRUNC _O_TRUNC
#define O_WRONLY _O_WRONLY
#define O_TEXT _O_TEXT
#define O_BINARY _O_BINARY
#endif
#define A_HIDDEN _A_HIDDEN
#define A_NORMAL _A_NORMAL
#define A_RDONLY _A_RDONLY
#define A_SUBDIR _A_SUBDIR
#else
#define A_SUBDIR 1
#define O_BINARY 0
#define O_TEXT 0
#endif /* ABYSS_WIN32 */
#ifdef ABYSS_WIN32
#ifndef __BORLANDC__
typedef struct _stati64 TFileStat;
typedef struct _finddata_t TFileInfo;
typedef long TFileFind;
#else
typedef struct stat TFileStat;
typedef struct finddata_t
{
char name[NAME_MAX+1];
int attrib;
uint64 size;
time_t time_write;
WIN32_FIND_DATA data;
} TFileInfo;
typedef HANDLE TFileFind;
#endif
#else
#include <unistd.h>
#include <dirent.h>
typedef struct stat TFileStat;
typedef struct finddata_t
{
char name[NAME_MAX+1];
int attrib;
uint64 size;
time_t time_write;
} TFileInfo;
typedef struct
{
char path[NAME_MAX+1];
DIR *handle;
} TFileFind;
#endif
typedef int TFile;
abyss_bool FileOpen(TFile *f, const char *name,uint32 attrib);
abyss_bool FileOpenCreate(TFile *f, const char *name, uint32 attrib);
abyss_bool FileClose(TFile *f);
abyss_bool FileWrite(TFile *f, void *buffer, uint32 len);
int32 FileRead(TFile *f, void *buffer, uint32 len);
abyss_bool FileSeek(TFile *f, uint64 pos, uint32 attrib);
uint64 FileSize(TFile *f);
abyss_bool FileStat(char *filename,TFileStat *filestat);
abyss_bool FileFindFirst(TFileFind *filefind,char *path,TFileInfo *fileinfo);
abyss_bool FileFindNext(TFileFind *filefind,TFileInfo *fileinfo);
void FileFindClose(TFileFind *filefind);
/*********************************************************************
** Server (1/2)
*********************************************************************/
typedef struct _TServer
{
TSocket listensock;
TFile logfile;
TMutex logmutex;
char *name;
char *filespath;
uint16 port;
uint32 keepalivetimeout,keepalivemaxconn,timeout;
TList handlers;
TList defaultfilenames;
void *defaulthandler;
abyss_bool advertise;
#ifndef _WIN32
uid_t uid;
gid_t gid;
TFile pidfile;
#endif
} TServer;
/*********************************************************************
** Conn
*********************************************************************/
#define BUFFER_SIZE 4096
typedef struct _TConn
{
TServer *server;
uint32 buffersize,bufferpos;
uint32 inbytes,outbytes;
TSocket socket;
TIPAddr peerip;
abyss_bool hasOwnThread;
TThread thread;
abyss_bool connected;
abyss_bool inUse;
void (*job)(struct _TConn *);
char buffer[BUFFER_SIZE];
} TConn;
TConn *ConnAlloc(void);
void ConnFree(TConn *c);
enum abyss_foreback {ABYSS_FOREGROUND, ABYSS_BACKGROUND};
abyss_bool ConnCreate(TConn *c, TSocket *s, void (*func)(TConn *));
abyss_bool ConnCreate2(TConn * const connectionP,
TServer * const serverP,
TSocket const connectedSocket,
TIPAddr const peerIpAddr,
void ( * func)(TConn *),
enum abyss_foreback const foregroundBackground);
abyss_bool ConnProcess(TConn *c);
abyss_bool ConnKill(TConn *c);
void ConnClose(TConn *c);
abyss_bool ConnWrite(TConn *c,void *buffer,uint32 size);
abyss_bool ConnRead(TConn *c, uint32 timems);
void ConnReadInit(TConn *c);
abyss_bool ConnReadLine(TConn *c,char **z,uint32 timems);
abyss_bool ConnWriteFromFile(TConn *c,TFile *file,uint64 start,uint64 end,
void *buffer,uint32 buffersize,uint32 rate);
/*********************************************************************
** Range
*********************************************************************/
abyss_bool RangeDecode(char *str,uint64 filesize,uint64 *start,uint64 *end);
/*********************************************************************
** Date
*********************************************************************/
#include <time.h>
typedef struct tm TDate;
abyss_bool DateToString(TDate *tm,char *s);
abyss_bool DateToLogString(TDate *tm,char *s);
abyss_bool DateDecode(char *s,TDate *tm);
int32 DateCompare(TDate *d1,TDate *d2);
abyss_bool DateFromGMT(TDate *d,time_t t);
abyss_bool DateFromLocal(TDate *d,time_t t);
abyss_bool DateInit(void);
/*********************************************************************
** Base64
*********************************************************************/
void Base64Encode(char *s,char *d);
/*********************************************************************
** Session
*********************************************************************/
typedef enum
{
m_unknown,m_get,m_put,m_head,m_post,m_delete,m_trace,m_options
} TMethod;
typedef struct
{
TMethod method;
uint32 nbfileds;
char *uri;
char *query;
char *host;
char *from;
char *useragent;
char *referer;
char *requestline;
char *user;
uint16 port;
TList cookies;
TList ranges;
uint16 status;
TString header;
abyss_bool keepalive,cankeepalive;
abyss_bool done;
TServer *server;
TConn *conn;
uint8 versionminor,versionmajor;
TTable request_headers,response_headers;
TDate date;
abyss_bool chunkedwrite,chunkedwritemode;
} TSession;
/*********************************************************************
** Request
*********************************************************************/
#define CR '\r'
#define LF '\n'
#define CRLF "\r\n"
abyss_bool RequestValidURI(TSession *r);
abyss_bool RequestValidURIPath(TSession *r);
abyss_bool RequestUnescapeURI(TSession *r);
char *RequestHeaderValue(TSession *r,char *name);
abyss_bool RequestRead(TSession *r);
void RequestInit(TSession *r,TConn *c);
void RequestFree(TSession *r);
abyss_bool RequestAuth(TSession *r,char *credential,char *user,char *pass);
/*********************************************************************
** Response
*********************************************************************/
abyss_bool ResponseAddField(TSession *r,char *name,char *value);
void ResponseWrite(TSession *r);
abyss_bool ResponseChunked(TSession *s);
void ResponseStatus(TSession *r,uint16 code);
void ResponseStatusErrno(TSession *r);
abyss_bool ResponseContentType(TSession *r,char *type);
abyss_bool ResponseContentLength(TSession *r,uint64 len);
void ResponseError(TSession *r);
/*********************************************************************
** HTTP
*********************************************************************/
char *HTTPReasonByStatus(uint16 status);
int32 HTTPRead(TSession *s,char *buffer,uint32 len);
abyss_bool HTTPWrite(TSession *s,char *buffer,uint32 len);
abyss_bool HTTPWriteEnd(TSession *s);
/*********************************************************************
** Server (2/2)
*********************************************************************/
typedef abyss_bool (*URIHandler) (TSession *);
abyss_bool ServerCreate(TServer *srv,
const char *name,
uint16 port,
const char *filespath,
const char *logfilename);
void ServerFree(TServer *srv);
void ServerInit(TServer *srv);
void ServerRun(TServer *srv);
void ServerRunOnce(TServer *srv);
void ServerRunOnce2(TServer * const srv,
enum abyss_foreback const foregroundBackground);
abyss_bool ServerAddHandler(TServer *srv,URIHandler handler);
void ServerDefaultHandler(TServer *srv,URIHandler handler);
abyss_bool LogOpen(TServer *srv, const char *filename);
void LogWrite(TServer *srv,char *c);
void LogClose(TServer *srv);
/*********************************************************************
** MIMEType
*********************************************************************/
void MIMETypeInit(void);
abyss_bool MIMETypeAdd(char *type,char *ext);
char *MIMETypeFromExt(char *ext);
char *MIMETypeFromFileName(char *filename);
char *MIMETypeGuessFromFile(char *filename);
/*********************************************************************
** Conf
*********************************************************************/
abyss_bool ConfReadMIMETypes(char *filename);
abyss_bool ConfReadServerFile(const char *filename,TServer *srv);
/*********************************************************************
** Trace
*********************************************************************/
void TraceMsg(char *fmt,...);
void TraceExit(char *fmt,...);
/*********************************************************************
** Session
*********************************************************************/
abyss_bool SessionLog(TSession *s);
#ifdef __cplusplus
}
#endif
#endif /* _ABYSS_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -