📄 ghttp.h
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#ifndef __GHTTP_H__#define __GHTTP_H__#include <time.h>class GHttpClientSocket;class GSocketServer;class GPointerArray;class GHttpServerBuffer;class GQueue;class GStringHeap;class GConstStringHashTable;// This class allows you to get files using the HTTP protocolclass GHttpClient{public: enum Status { Downloading, Error, NotFound, Done, Aborted, };protected: char m_szHeaderBuf[258]; char m_szServer[256]; char m_szClientName[32]; int m_nHeaderPos; int m_nContentSize; bool m_bChunked; unsigned char* m_pData; int m_nDataPos; GHttpClientSocket* m_pSocket; Status m_status; GQueue* m_pChunkQueue; bool m_bPastHeader; char* m_szRedirect; double m_dLastReceiveTime; char caLastModifiedString[60]; bool m_bAmCurrentlyDoingJustHeaders;public: GHttpClient(); ~GHttpClient(); // Send a request to get a file. Returns immediately (before the file // is downloaded). bool Get(const char* szUrl, bool actuallyDownloadTheData = true); // See what the status of the download is. If everything is going okay, // it will return "Downloading" while downloading and "Done" when the file // is available. pfProgress is an optional parameter. If it is non-NULL, // it will return a number between 0 and 1 that indicates the ratio of // content (not including header data) already downloaded. Status CheckStatus(float* pfProgress); // Don't call this until the status is "Done". It returns a pointer to the // file that was downloaded. The buffer will be deleted when this object is // deleted, so if you want to retain the buffer, call DropData instead. unsigned char* GetData(int* pnSize); //get the date string of this char* GetUrlModified() { /* todor check first */ return caLastModifiedString; } // Just like GetData except it forgets about the buffer so you'll have to // delete it yourself. unsigned char* DropData(int* pnSize); // This is called when the connection is lost void OnLoseConnection(); void SetClientName(const char* szClientName); void Abort(); // called by the consumer, when an abort is desired. protected: void ProcessHeader(const unsigned char* szData, int nSize); void ProcessBody(const unsigned char* szData, int nSize); void ProcessChunkBody(const unsigned char* szData, int nSize); void GimmeWhatYouGot();};#define MAX_SERVER_LINE_SIZE 300#define MAX_COOKIE_SIZE 300// This class allows you to implement a simple HTTP daemonclass GHttpServer{protected: GSocketServer* m_pSocket; GPointerArray* m_pBuffers; GQueue* m_pQ; char m_szContentType[64]; char m_szCookie[MAX_COOKIE_SIZE]; bool m_bPersistCookie; time_t m_time;public: GHttpServer(int nPort); virtual ~GHttpServer(); // You should call this method constantly inside the main loop. // It returns true if it did anything, and false if it didn't, so // if it returns false you may want to sleep for a little while. bool Process(); // Unescapes a URL. (i.e. replace "%20" with " ", etc.) static void UnescapeUrl(char* szOut, const char* szIn); // Parses the parameters in a URL and puts them in a table static void ParseParams(GStringHeap* pStringHeap, GConstStringHashTable* pTable, const char* szParams); // Specifies the content-type of the response void SetContentType(const char* szContentType); // Specifies the set-cookie header to be sent with the response void SetCookie(const char* szPayload, bool bPersist); // Sets the date (modified time) to be sent with the file so the client can cache it void SetTimeStamp(time_t t) { m_time = t; }protected: virtual void OnProcessLine(int nConnection, const char* szLine) {} void ProcessPostData(int nConnection, GHttpServerBuffer* pClient, const unsigned char* pData, int nDataSize); void ProcessHeaderLine(int nConnection, GHttpServerBuffer* pClient, const char* szLine); void BeginRequest(GHttpServerBuffer* pClient, int eType, const char* szIn); void SendResponse(GHttpServerBuffer* pClient, int nConnection); void SendNotModifiedResponse(GHttpServerBuffer* pClient, int nConnection); // This method should set the content type and the date headers, and any other // headers deemed necessary virtual void SetHeaders(const char* szUrl, const char* szParams) = 0; // The primary purpose of this method is to push a response into pResponse. // Typically this method will call SetHeaders. virtual void DoGet(const char* szUrl, const char* szParams, int nParamsLen, const char* szCookie, GQueue* pResponse) = 0; // This method takes ownership of pData. Don't forget to delete it. When the POST is // caused by an HTML form, it's common for this method to just call DoGet (passing // pData for szParams) and then delete pData. (For convenience, a '\0' is already appended // at the end of pData.) virtual void DoPost(const char* szUrl, unsigned char* pData, int nDataSize, const char* szCookie, GQueue* pResponse) = 0; // This is called when the client does a conditional GET. It should return true // if you wish to re-send the file, and DoGet will be called. virtual bool HasBeenModifiedSince(const char* szUrl, const char* szDate) = 0;};#endif // __GHTTP_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -