?? httpapi.h
字號:
/////////////////////////////////////////////////////////////////////////// httpapi.h//// External API header file for http protocol/////////////////////////////////////////////////////////////////////////#ifndef _HTTPAPI_H_#define _HTTPAPI_H_#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <time.h>#define VER_MAJOR 0#define VER_MINOR 7#ifndef min#define min(x,y) (x>y?y:x)#endif#ifndef NODEBUG#define DEBUG printf#else#define DEBUG#endif#define SYSLOG fprintf#define LOG_ERR 1#define ASSERT#define GETDWORD(ptrData) (*(DWORD*)(ptrData))#define SETDWORD(ptrData,data) (*(DWORD*)(ptrData)=data)#define GETWORD(ptrData) (*(WORD*)(ptrData))#define SETWORD(ptrData,data) (*(WORD*)(ptrData)=data)#ifndef BIG_ENDINE#define DEFDWORD(char1,char2,char3,char4) (char1+(char2<<8)+(char3<<16)+(char4<<24))#define DEFWORD(char1,char2) (char1+(char2<<8))#else#define DEFDWORD(char1,char2,char3,char4) (char4+(char3<<8)+(char2<<16)+(char1<<24))#define DEFWORD(char1,char2) (char2+(char1<<8))#endif///////////////////////////////////////////////////////////////////////// Public definitions///////////////////////////////////////////////////////////////////////// file typestypedef enum { HTTPFILETYPE_HTML=0, HTTPFILETYPE_TEXT, HTTPFILETYPE_GIF, HTTPFILETYPE_JPEG, HTTPFILETYPE_PNG, HTTPFILETYPE_CSS, HTTPFILETYPE_SWF, HTTPFILETYPE_MPA, HTTPFILETYPE_MPEG, HTTPFILETYPE_JS, HTTPFILETYPE_OCTET, HTTPFILETYPE_STREAM,} HttpFileType;#define MAXPOSTPARAMS 50#define MAXPOSTREDIRECTFILENAME (200)/////////////////////////////////////////////////////////////////////////////// typedefs/////////////////////////////////////////////////////////////////////////////typedef struct _tagPostParam { // char* pchPageName; struct { char* pchParamName; char* pchParamValue; } stParams[MAXPOSTPARAMS]; int iNumParams; char chFilename[MAXPOSTREDIRECTFILENAME];} PostParam;// multipart file upload post (per socket) structuretypedef struct { char pchBoundaryValue[80]; OCTET oFileuploadStatus; int iWriteLocation; PostParam pp; char *pchFilename;} HttpMultipart;typedef struct _tagSubstParam { char* pchParamName; char* pchParamValue; // returned int iMaxValueBytes;} SubstParam;#define FLAG_REQUEST_GET 0x1#define FLAG_REQUEST_POST 0x2#define FLAG_CONN_CLOSE 0x10#define FLAG_SUBST 0x20#define FLAG_AUTHENTICATION 0x40#define FLAG_MORE_CONTENT 0x80#define FLAG_TO_FREE 0x100#define FLAG_DATA_FILE 0x10000#define FLAG_DATA_RAW 0x20000#define FLAG_DATA_PIPE 0x40000#define FLAG_DATA_STREAM 0x80000#define FLAG_RECEIVING 0x80000000#define FLAG_SENDING 0x40000000#define SETFLAG(hs,bit) (hs->flags|=(bit));#define CLRFLAG(hs,bit) (hs->flags&=~(bit));#define ISFLAGSET(hs,bit) ((hs->flags&(bit)))typedef union { unsigned long laddr; unsigned short saddr[2]; unsigned char caddr[4];} IP;typedef struct { IP ipAddr; int iStartByte; unsigned char *pucPath; short int ofReferer; short int ofHost; short int siHeaderSize;#ifdef HTTPPOST unsigned char* pucPayload;#endif} HttpRequest;typedef struct { int iSentBytes; int iContentLength; HttpFileType fileType; short int iBufferSize; // the size of buffer pucData pointing to} HttpResponse;typedef struct { char *name; char *value;} HttpVariables;typedef struct { void* hp; unsigned char *pucRequest; HttpVariables* pxVars; int iVarCount; unsigned char *pucHeader; HttpRequest *request; unsigned char *pucBuffer; int iDataBytes; int iContentBytes; int iSentBytes; HttpFileType fileType;} UrlHandlerParam;// Callback function protostypedef int (*PFNPOSTCALLBACK)(PostParam*);typedef int (*PFNSUBSTCALLBACK)(SubstParam*);typedef int (*PFNFILEUPLOADCALLBACK)(char*, OCTET, OCTET*, DWORD);typedef int (*PFNURLCALLBACK)(UrlHandlerParam*);typedef int (*PFNURLINIT)(void* hp, int uninit);typedef struct { char* pchUrlPrefix; PFNURLCALLBACK pfnUrlHandler; PFNURLINIT pfnUrlHandlerInit;} UrlHandler;typedef struct { time_t startTime; int clientCount; int clientCountMax; int reqCount; int reqGetCount; int fileSentCount; int fileSentBytes; int varSubstCount; int urlProcessCount; int timeOutCount;#ifdef HTTPAUTH int authFailCount;#endif#ifdef HTTPPOST int reqPostCount; int fileUploadCount;#endif} HttpStats;#define HTTP_BUFFER_SIZE (4*1024 /*bytes*/)// per connection/socket structuretypedef struct _HttpSocket{ struct _HttpSocket *next; SOCKET socket; int fd; HttpRequest request; HttpResponse response; unsigned char *pucData; int iDataLength; unsigned long flags; void* ptr; time_t tmAcceptTime; time_t tmExpirationTime; unsigned short siRequestCount; unsigned short ucReserved; unsigned char buffer[HTTP_BUFFER_SIZE];} HttpSocket;#define FLAG_DIR_LISTING 1typedef struct _httpParam { HttpSocket *phsSocketHead; /* head of the socket linked list */ int bKillWebserver; int bWebserverRunning; unsigned int flags; SOCKET listenSocket; unsigned short int siHttpPort; unsigned short int siMaxReqPerConn; /* maximum requests on one connection */ unsigned short int siMaxClients; unsigned short int siSocketRcvBufSize; /* socket receive buffer size in KB */ char *pchWebPath; UrlHandler *pxUrlHandler; /* pointer to URL handler array */ // substitution callback PFNSUBSTCALLBACK pfnSubst;#ifdef HTTPPOST // post callbacks PFNFILEUPLOADCALLBACK pfnFileUpload; PFNPOSTCALLBACK pfnPost;#endif#ifdef HTTPAUTH DWORD dwAuthenticatedNode; time_t tmAuthExpireTime;#endif #ifndef NOTHREAD pthread_t tidHttpThread; #endif HttpStats stats;} HttpParam;typedef struct { char* pchRootPath; char* pchHttpPath; char cFilePath[MAX_PATH]; char* pchExt; int fTailSlash;} HttpFilePath;///////////////////////////////////////////////////////////////////////// Return codes///////////////////////////////////////////////////////////////////////// for post callback#define WEBPOST_OK (0)#define WEBPOST_AUTHENTICATED (1)#define WEBPOST_NOTAUTHENTICATED (2)#define WEBPOST_AUTHENTICATIONON (3)#define WEBPOST_AUTHENTICATIONOFF (4)// for multipart file uploads#define HTTPUPLOAD_MORECHUNKS (0)#define HTTPUPLOAD_FIRSTCHUNK (1)#define HTTPUPLOAD_LASTCHUNK (2)///////////////////////////////////////////////////////////////////////// Public functions//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// mwServerStart. Startup the webserver///////////////////////////////////////////////////////////////////////int mwServerStart(HttpParam* hp);///////////////////////////////////////////////////////////////////////// mwServerShutdown. Shutdown the webserver (closes connections and// releases resources)///////////////////////////////////////////////////////////////////////int mwServerShutdown(HttpParam* hp);///////////////////////////////////////////////////////////////////////// mwSetRcvBufSize. Change the TCP windows size of acceped sockets///////////////////////////////////////////////////////////////////////int mwSetRcvBufSize(WORD wSize);///////////////////////////////////////////////////////////////////////// mwPostRegister. Specify the callback to be called when a POST is// recevied.///////////////////////////////////////////////////////////////////////PFNPOSTCALLBACK mwPostRegister(PFNPOSTCALLBACK);///////////////////////////////////////////////////////////////////////// mwFileUploadRegister. Specify the callback to be called whenever the // server has the next data chunk available from a multipart file upload.///////////////////////////////////////////////////////////////////////PFNFILEUPLOADCALLBACK mwFileUploadRegister(PFNFILEUPLOADCALLBACK);///////////////////////////////////////////////////////////////////////// Default subst, post and file-upload callback processing///////////////////////////////////////////////////////////////////////int DefaultWebSubstCallback(SubstParam* sp);int DefaultWebPostCallback(PostParam* pp);int DefaultWebFileUploadCallback(char *pchFilename, OCTET oFileuploadStatus, OCTET *poData, DWORD dwDataChunkSize);int mwGetHttpDateTime(time_t tm, char *buf);int mwGetLocalFileName(HttpFilePath* hfp);char* mwGetVarValue(HttpVariables* vars, char *varname);#endif // _HTTPAPI_H////////////////////////// END OF FILE ////////////////////////////////
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -