⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpapi.h

📁 小型的web服务器源码
💻 H
字号:
/////////////////////////////////////////////////////////////////////////// httpapi.h//// External API header file for http protocol (webserver)/////////////////////////////////////////////////////////////////////////#ifndef _HTTPAPI_H_#define _HTTPAPI_H_#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>typedef unsigned char OCTET;typedef unsigned short WORD;typedef unsigned long DWORD;typedef int BOOL;#define FALSE 0#define TRUE 1#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 HTTPPOST//#define HTTPAUTH///////////////////////////////////////////////////////////////////////// Public definitions///////////////////////////////////////////////////////////////////////// file typestypedef enum {  HTTPFILETYPE_HTML=0,  HTTPFILETYPE_TEXT,  HTTPFILETYPE_GIF,  HTTPFILETYPE_JPEG,  HTTPFILETYPE_PNG,  HTTPFILETYPE_JS,  HTTPFILETYPE_CSS,  HTTPFILETYPE_SWF,  HTTPFILETYPE_OCTET,} HttpFileType;// file sourcestypedef enum {  HTTPSRC_DEFAULT=0,  HTTPSRC_FILE,  HTTPSRC_MEM,  HTTPSRC_PIPE,  HTTPSRC_NONE,} HttpContentSource;#define MAXPOSTPARAMS 50#define MAXPOSTREDIRECTFILENAME (200)#define HTTPURLSIZE 32/////////////////////////////////////////////////////////////////////////////// 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;// socket statestypedef enum {  HTTPSOCKETSTATE_NOTINUSE=0,  HTTPSOCKETSTATE_LISTENING,  HTTPSOCKETSTATE_SENDING,  HTTPSOCKETSTATE_RECEIVING,} HttpSocketState;// request typetypedef enum {  HTTPREQTYPE_UNKNOWN=0,  HTTPREQTYPE_GET,  HTTPREQTYPE_POST,} HttpReqType;#define HEADER_DATA_MAX_SIZE 256#define FLAG_KEEPALIVE 1// per connection/socket structuretypedef struct {  SOCKET iSocket;  HttpSocketState hssState;  HttpReqType hrtType;  unsigned short sKeepAlive;  unsigned short sFlags;  long lExpirationTime;  unsigned char* pucData;  int iDataLength;  int iBufferLength;  int iExpansionLeft;  int iFile;  HttpFileType hftFileType;  BOOL bSubstitutionNeeded;  BOOL bHeaderSent;  BOOL bRawDataSend;  HttpContentSource hcsSource;  HttpMultipart *pxMultipartInfo;  unsigned char *pucRequest;  unsigned char *pucReferer;  unsigned char *pucHost;  unsigned char aucHeaderData[HEADER_DATA_MAX_SIZE];  unsigned char *pucFreeSpace;} HttpSocket;typedef struct {	int startTime;	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 MAXSUBSTLENGTH 200typedef struct _tagSubstParam {  char* pchParamName;  char chParamValue[MAXSUBSTLENGTH]; // returned} SubstParam;// Callback function protostypedef int (*PFNPOSTCALLBACK)(PostParam*);typedef int (*PFNSUBSTCALLBACK)(SubstParam*);typedef int (*PFNFILEUPLOADCALLBACK)(char*, OCTET, OCTET*, DWORD);typedef int (*PFNURLCALLBACK)(HttpSocket*,char*);typedef struct _httpParam {  short int httpPort;  char *webPath;} httpParam;///////////////////////////////////////////////////////////////////////// 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*);///////////////////////////////////////////////////////////////////////// mwServerShutdown. Shutdown the webserver (closes connections and// releases resources)///////////////////////////////////////////////////////////////////////int mwServerShutdown();///////////////////////////////////////////////////////////////////////// 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);///////////////////////////////////////////////////////////////////////// mwSubstRegister. Specify the callback to be called whenever the server// needs a substitution value for a variable. ($$VARNAME$$ in the html)///////////////////////////////////////////////////////////////////////PFNSUBSTCALLBACK mwSubstRegister(PFNSUBSTCALLBACK);int mwUrlProcessorRegister(char *pchUrlBegin, PFNURLCALLBACK pfnCallback);///////////////////////////////////////////////////////////////////////// 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);HttpStats* mwGetHttpStats();#endif // _HTTPAPI_H////////////////////////// END OF FILE ////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -