| 1 | // -*- mode: cpp; mode: fold -*- |
| 2 | // Description /*{{{*/// $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $ |
| 3 | // $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $ |
| 4 | /* ###################################################################### |
| 5 | |
| 6 | HTTP Acquire Method - This is the HTTP acquire method for APT. |
| 7 | |
| 8 | ##################################################################### */ |
| 9 | /*}}}*/ |
| 10 | |
| 11 | #ifndef APT_HTTP_H |
| 12 | #define APT_HTTP_H |
| 13 | |
| 14 | #include <apt-pkg/strutl.h> |
| 15 | #include <apt-pkg/acquire-method.h> |
| 16 | |
| 17 | #include <string> |
| 18 | #include <sys/time.h> |
| 19 | #include <iostream> |
| 20 | |
| 21 | #include "server.h" |
| 22 | |
| 23 | using std::cout; |
| 24 | using std::endl; |
| 25 | |
| 26 | class FileFd; |
| 27 | class HttpMethod; |
| 28 | class Hashes; |
| 29 | |
| 30 | class CircleBuf |
| 31 | { |
| 32 | unsigned char *Buf; |
| 33 | unsigned long long Size; |
| 34 | unsigned long long InP; |
| 35 | unsigned long long OutP; |
| 36 | std::string OutQueue; |
| 37 | unsigned long long StrPos; |
| 38 | unsigned long long MaxGet; |
| 39 | struct timeval Start; |
| 40 | |
| 41 | static unsigned long long BwReadLimit; |
| 42 | static unsigned long long BwTickReadData; |
| 43 | static struct timeval BwReadTick; |
| 44 | static const unsigned int BW_HZ; |
| 45 | |
| 46 | unsigned long long LeftRead() const |
| 47 | { |
| 48 | unsigned long long Sz = Size - (InP - OutP); |
| 49 | if (Sz > Size - (InP%Size)) |
| 50 | Sz = Size - (InP%Size); |
| 51 | return Sz; |
| 52 | } |
| 53 | unsigned long long LeftWrite() const |
| 54 | { |
| 55 | unsigned long long Sz = InP - OutP; |
| 56 | if (InP > MaxGet) |
| 57 | Sz = MaxGet - OutP; |
| 58 | if (Sz > Size - (OutP%Size)) |
| 59 | Sz = Size - (OutP%Size); |
| 60 | return Sz; |
| 61 | } |
| 62 | void FillOut(); |
| 63 | |
| 64 | public: |
| 65 | Hashes *Hash; |
| 66 | // total amount of data that got written so far |
| 67 | unsigned long long TotalWriten; |
| 68 | |
| 69 | // Read data in |
| 70 | bool Read(int Fd); |
| 71 | bool Read(std::string Data); |
| 72 | |
| 73 | // Write data out |
| 74 | bool Write(int Fd); |
| 75 | bool WriteTillEl(std::string &Data,bool Single = false); |
| 76 | |
| 77 | // Control the write limit |
| 78 | void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;} |
| 79 | bool IsLimit() const {return MaxGet == OutP;}; |
| 80 | void Print() const {cout << MaxGet << ',' << OutP << endl;}; |
| 81 | |
| 82 | // Test for free space in the buffer |
| 83 | bool ReadSpace() const {return Size - (InP - OutP) > 0;}; |
| 84 | bool WriteSpace() const {return InP - OutP > 0;}; |
| 85 | |
| 86 | void Reset(); |
| 87 | // Dump everything |
| 88 | void Stats(); |
| 89 | |
| 90 | CircleBuf(unsigned long long Size); |
| 91 | ~CircleBuf(); |
| 92 | }; |
| 93 | |
| 94 | struct HttpServerState: public ServerState |
| 95 | { |
| 96 | // This is the connection itself. Output is data FROM the server |
| 97 | CircleBuf In; |
| 98 | CircleBuf Out; |
| 99 | int ServerFd; |
| 100 | |
| 101 | protected: |
| 102 | virtual bool ReadHeaderLines(std::string &Data); |
| 103 | virtual bool LoadNextResponse(bool const ToFile, FileFd * const File); |
| 104 | virtual bool WriteResponse(std::string const &Data); |
| 105 | |
| 106 | public: |
| 107 | virtual void Reset() { ServerState::Reset(); ServerFd = -1; }; |
| 108 | |
| 109 | virtual bool RunData(FileFd * const File); |
| 110 | |
| 111 | virtual bool Open(); |
| 112 | virtual bool IsOpen(); |
| 113 | virtual bool Close(); |
| 114 | virtual bool InitHashes(FileFd &File); |
| 115 | virtual Hashes * GetHashes(); |
| 116 | virtual bool Die(FileFd &File); |
| 117 | virtual bool Flush(FileFd * const File); |
| 118 | virtual bool Go(bool ToFile, FileFd * const File); |
| 119 | |
| 120 | HttpServerState(URI Srv, HttpMethod *Owner); |
| 121 | virtual ~HttpServerState() {Close();}; |
| 122 | }; |
| 123 | |
| 124 | class HttpMethod : public ServerMethod |
| 125 | { |
| 126 | public: |
| 127 | virtual void SendReq(FetchItem *Itm); |
| 128 | |
| 129 | virtual bool Configuration(std::string Message); |
| 130 | |
| 131 | virtual ServerState * CreateServerState(URI uri); |
| 132 | virtual void RotateDNS(); |
| 133 | |
| 134 | protected: |
| 135 | std::string AutoDetectProxyCmd; |
| 136 | |
| 137 | public: |
| 138 | friend struct HttpServerState; |
| 139 | |
| 140 | HttpMethod() : ServerMethod("1.2",Pipeline | SendConfig) |
| 141 | { |
| 142 | File = 0; |
| 143 | Server = 0; |
| 144 | }; |
| 145 | }; |
| 146 | |
| 147 | #endif |