ReactOS 0.4.15-dev-7934-g1dc8d80
CHttpClient Class Reference

#include <httpd.h>

Inheritance diagram for CHttpClient:
Collaboration diagram for CHttpClient:

Public Member Functions

 CHttpClient ()
 
 CHttpClient (LPCServerSocket lpServerSocket)
 
virtual void OnRead ()
 
virtual void OnClose ()
 
void SplitUri (const LPSTR lpsUri, LPSTR lpsHost, LPSTR lpsResource, LPSTR lpsParams)
 
void SplitResource (const LPSTR lpsResource, LPSTR lpsPath, LPSTR lpsFilename, LPSTR lpsExtension)
 
void ProcessRequest ()
 
void SendFile (const LPSTR lpsFilename)
 
void Report (LPCSTR lpsCode, const LPSTR lpsStr)
 
- Public Member Functions inherited from CServerClientSocket
 CServerClientSocket ()
 
 CServerClientSocket (LPCServerSocket lpServerSocket)
 
CServerSocketGetServerSocket ()
 
virtual INT Transmit (LPCSTR lpsBuffer, UINT nLength)
 
virtual INT Receive (LPSTR lpsBuffer, UINT nLength)
 
virtual INT SendText (LPCSTR lpsText)
 
virtual VOID MessageLoop ()
 
virtual VOID OnRead ()
 
virtual VOID OnClose ()
 
- Public Member Functions inherited from CSocket
 CSocket ()
 
virtual ~CSocket ()
 
virtual SOCKET GetSocket ()
 
virtual VOID SetSocket (SOCKET socket)
 
virtual SOCKADDR_IN GetSockAddrIn ()
 
virtual VOID SetSockAddrIn (SOCKADDR_IN sockaddrin)
 
virtual VOID SetEvents (LONG lEvents)
 
virtual LONG GetEvents ()
 
virtual VOID SetPort (UINT nPort)
 
virtual VOID Open ()
 
virtual VOID Close ()
 
virtual INT Transmit (LPCSTR lpsBuffer, UINT nLength)
 
virtual INT Receive (LPSTR lpsBuffer, UINT nLength)
 
virtual INT SendText (LPCSTR lpsStr)
 

Public Attributes

HANDLE ThreadHandle
 
DWORD ThreadId
 
CHttpParser Parser
 

Private Attributes

BOOL bStop
 
LPSTR lpsBuffer
 
LONG nBufferSize
 
unsigned long long nTotalRead
 
unsigned long long nFileSize
 
HANDLE hFile
 

Additional Inherited Members

- Protected Attributes inherited from CServerClientSocket
LPCServerSocket ServerSocket
 
- Protected Attributes inherited from CSocket
SOCKET Socket
 
SOCKADDR_IN SockAddrIn
 
WSAEVENT Event
 
UINT Port
 
BOOL Active
 

Detailed Description

Definition at line 26 of file httpd.h.

Constructor & Destructor Documentation

◆ CHttpClient() [1/2]

CHttpClient::CHttpClient ( )

Definition at line 30 of file httpd.cpp.

31{
32}

◆ CHttpClient() [2/2]

CHttpClient::CHttpClient ( LPCServerSocket  lpServerSocket)

Member Function Documentation

◆ OnClose()

VOID CHttpClient::OnClose ( )
virtual

Reimplemented from CServerClientSocket.

Definition at line 334 of file httpd.cpp.

335{
336 // Stop sending file if we are doing that now
337 bStop = TRUE;
338}
BOOL bStop
Definition: httpd.h:42
#define TRUE
Definition: types.h:120

◆ OnRead()

VOID CHttpClient::OnRead ( )
virtual

Reimplemented from CServerClientSocket.

Definition at line 289 of file httpd.cpp.

290{
291 LONG nCount;
292
293 nCount = Receive((LPSTR) &Parser.sBuffer[Parser.nHead],
294 sizeof(Parser.sBuffer) - Parser.nHead);
295
296 Parser.nHead += nCount;
297 if (Parser.nHead >= sizeof(Parser.sBuffer))
298 Parser.nHead = 0;
299
300 if (Parser.Complete()) {
302 }
303
305 // Method Not Allowed
306 Report("405 Method Not Allowed", HttpMsg405);
307 // Terminate connection
308 Close();
309 }
310}
void ProcessRequest()
Definition: httpd.cpp:118
CHttpParser Parser
Definition: httpd.h:35
void Report(LPCSTR lpsCode, const LPSTR lpsStr)
Definition: httpd.cpp:263
CHAR sBuffer[2048]
Definition: http.h:28
UINT nHead
Definition: http.h:29
BOOL Complete()
Definition: http.cpp:49
BOOL bUnknownMethod
Definition: http.h:36
virtual INT Receive(LPSTR lpsBuffer, UINT nLength)
Definition: socket.cpp:126
virtual VOID Close()
Definition: socket.cpp:95
CHAR HttpMsg405[]
Definition: httpd.cpp:22
long LONG
Definition: pedump.c:60
char * LPSTR
Definition: xmlstorage.h:182

◆ ProcessRequest()

VOID CHttpClient::ProcessRequest ( )

Definition at line 118 of file httpd.cpp.

119{
120 CHAR sStr[255];
121 CHAR sHost[255];
122 CHAR sResource[255];
123 CHAR sParams[255];
124
125 // Which method?
126 switch (Parser.nMethodNo) {
127 case hmGET: {
128 SplitUri(Parser.sUri, sHost, sResource, sParams);
129
130 // Default resource?
131 if (strlen(sResource) == 0) {
133
134 // FIXME: All default resources should be tried
135 // Iterate through all strings
136 //for (i->First(); !i->IsDone(); i->Next())
137 i->First();
138 if (!i->IsDone()) {
139 strcat(sResource, i->CurrentItem());
140 delete i;
141 } else {
142 // File not found
143 Report("404 Not Found", HttpMsg404);
144 break;
145 }
146 }
148 strcat(sStr, sResource);
149 SendFile(sStr);
150 break;
151 }
152 default: {
153 // Method is not implemented
154 Report("501 Not Implemented", HttpMsg501);
155 }
156 }
157}
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
LPSTR GetHttpBase()
Definition: config.cpp:104
CList< LPSTR > * GetDefaultResources()
Definition: config.cpp:116
void SplitUri(const LPSTR lpsUri, LPSTR lpsHost, LPSTR lpsResource, LPSTR lpsParams)
Definition: httpd.cpp:41
void SendFile(const LPSTR lpsFilename)
Definition: httpd.cpp:160
CHAR sUri[255]
Definition: http.h:31
UINT nMethodNo
Definition: http.h:35
CIterator< Item > * CreateIterator() const
Definition: list.h:93
LPCConfig pConfiguration
Definition: config.cpp:18
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
CHAR HttpMsg501[]
Definition: httpd.cpp:24
CHAR HttpMsg404[]
Definition: httpd.cpp:21
#define hmGET
Definition: http.h:19
char CHAR
Definition: xmlstorage.h:175

Referenced by OnRead().

◆ Report()

VOID CHttpClient::Report ( LPCSTR  lpsCode,
const LPSTR  lpsStr 
)

Definition at line 263 of file httpd.cpp.

264{
265 CHAR sTmp[128];
266 CHAR sTmp2[16];
267
268 strcpy(sTmp, "HTTP/1.1 ");
269 strcat(sTmp, lpsCode);
270 SendText(sTmp);
271 SendText("Server: ROSHTTPD");
272 SendText("MIME-version: 1.0");
273 SendText("Content-Type: text/html");
274 SendText("Accept-Ranges: bytes");
275 strcpy(sTmp, "Content-Length: ");
276 if (lpsStr != NULL) {
277 _itoa(strlen(lpsStr), sTmp2, 10);
278 strcat(sTmp, sTmp2);
279 } else
280 strcat(sTmp, "0");
281 SendText(sTmp);
282 SendText("");
283 if (lpsStr != NULL)
284 SendText(lpsStr);
285 SendText("");
286}
virtual INT SendText(LPCSTR lpsText)
Definition: socket.cpp:115
#define NULL
Definition: types.h:112
_CRTIMP char *__cdecl _itoa(_In_ int _Value, _Pre_notnull_ _Post_z_ char *_Dest, _In_ int _Radix)

Referenced by OnRead(), ProcessRequest(), and SendFile().

◆ SendFile()

VOID CHttpClient::SendFile ( const LPSTR  lpsFilename)

Definition at line 160 of file httpd.cpp.

161{
162 CHAR str[255];
163 CHAR str2[32];
164 union BigNum {
165 // unsigned __int64 Big;
166 unsigned long long Big;
167 struct {
168 DWORD Low;
169 DWORD High;
170 } u;
171 } nTotalBytes;
172 DWORD nBytesToRead;
173 DWORD nBytesRead;
174 BOOL bStatus;
175
176 // Try to open file
177 hFile = CreateFileA(lpsFilename,
178 GENERIC_READ, // Open for reading
179 FILE_SHARE_READ, // Share for reading
180 NULL, // No security
181 OPEN_EXISTING, // Existing file only
182 FILE_ATTRIBUTE_NORMAL, // Normal file
183 NULL); // No attr. template
185 // File not found
186 Report("404 Not Found", HttpMsg404);
187 return;
188 }
189 // Get file size
190 nTotalBytes.u.Low = GetFileSize(hFile, &nTotalBytes.u.High);
191 if ((nTotalBytes.u.Low == 0xFFFFFFFF) && ((GetLastError()) != NO_ERROR)) {
192 // Internal server error
193 Report("500 Internal Server Error", HttpMsg500);
194 // Close file
196 return;
197 }
198
199 // Determine buffer size
200 if (nTotalBytes.Big < 65536)
201 nBufferSize = 1024;
202 else
203 nBufferSize = 32768;
204 // Allocate memory on heap
206
207 if (lpsBuffer == NULL) {
208 // Internal server error
209 Report("500 Internal Server Error", HttpMsg500);
210 // Close file
212 return;
213 }
214
215 SendText("HTTP/1.1 200 OK");
216 SendText("Server: ROSHTTPD");
217 SendText("MIME-version: 1.0");
218 SendText("Content-Type: text/plain");
219 SendText("Accept-Ranges: bytes");
220 strcpy(str, "Content-Length: ");
221 _itoa(nTotalBytes.u.Low, str2, 10);
222 strcat(str, str2);
223 SendText(str);
224 SendText("");
225 // Read and transmit file
226 nTotalRead = 0;
227 nFileSize = nTotalBytes.Big;
228 bStop = FALSE;
229
230 fd_set wfds;
231 FD_ZERO(&wfds);
232 FD_SET(Socket, &wfds);
233 do {
234 MessageLoop();
235
237 nBytesToRead = nBufferSize;
238 else nBytesToRead = nFileSize - nTotalRead;
239
240 bStatus = ReadFile(hFile, lpsBuffer, nBytesToRead, &nBytesRead, NULL);
241 if (bStatus) {
242 select(0, NULL, &wfds, NULL, NULL);
243 bStatus = (Transmit(lpsBuffer, nBytesRead) == (INT)nBytesRead);
244 nTotalRead += nBytesRead;
245 }
246 } while ((!bStop) && (bStatus) && (nTotalRead < nFileSize));
247
248 if (bStatus)
249 SendText("");
250 else
251 // We can't send an error message here as we are in the process of sending a file.
252 // We have to terminate the connection instead
253 Close();
254
255 // Free allocated memory
257
258 // Close file
260}
unsigned long long nTotalRead
Definition: httpd.h:46
unsigned long long nFileSize
Definition: httpd.h:48
LPSTR lpsBuffer
Definition: httpd.h:43
LONG nBufferSize
Definition: httpd.h:44
virtual INT Transmit(LPCSTR lpsBuffer, UINT nLength)
Definition: socket.cpp:109
virtual VOID MessageLoop()
Definition: socket.cpp:132
SOCKET Socket
Definition: socket.h:89
#define NO_ERROR
Definition: dderror.h:5
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:740
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
CHAR HttpMsg500[]
Definition: httpd.cpp:23
#define PCHAR
Definition: match.c:90
_In_ HANDLE hFile
Definition: mswsock.h:90
#define INT
Definition: polytest.cpp:20
const WCHAR * str
@ High
Definition: strmini.h:378
@ Low
Definition: strmini.h:380
Definition: winsock.h:66
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89

Referenced by ProcessRequest().

◆ SplitResource()

VOID CHttpClient::SplitResource ( const LPSTR  lpsResource,
LPSTR  lpsPath,
LPSTR  lpsFilename,
LPSTR  lpsExtension 
)

Definition at line 80 of file httpd.cpp.

81{
82 INT i,len,fileptr,extptr;
83
84 strcpy(lpsPath, "");
85 strcpy(lpsFilename, "");
86 strcpy(lpsExtension, "");
87
88 len = strlen(lpsResource);
89 if (len != 0) {
90 if (lpsResource[len - 1] == '/') {
91 // There is only a path
92 strcpy(lpsPath, lpsResource);
93 } else {
94 // Find extension
95 i = len - 1;
96 while ((i >= 0) && (lpsResource[i] != '.')) i--;
97 extptr = i;
98 while ((i >= 0) && (lpsResource[i] != '/')) i--;
99 if (i > 0) {
100 // There is at least one directory in the path (besides root directory)
101 fileptr = i + 1;
102 strncat(lpsPath, lpsResource, fileptr);
103 } else
104 fileptr = 1;
105
106 // Get filename and possibly extension
107 if (extptr != 0) {
108 strncat(lpsFilename, &lpsResource[fileptr], extptr - fileptr);
109 // Get extension
110 strncat(lpsExtension, &lpsResource[extptr + 1], len - extptr - 1);
111 } else
112 strncat(lpsFilename, &lpsResource[fileptr], len - fileptr);
113 }
114 }
115}
char * strncat(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:605
GLenum GLsizei len
Definition: glext.h:6722
int32_t INT
Definition: typedefs.h:58

◆ SplitUri()

VOID CHttpClient::SplitUri ( const LPSTR  lpsUri,
LPSTR  lpsHost,
LPSTR  lpsResource,
LPSTR  lpsParams 
)

Definition at line 41 of file httpd.cpp.

42{
43 LPSTR lpsPos;
44 LPSTR lpsStr;
45 UINT i;
46
47 strcpy(lpsHost, "");
48 strcpy(lpsResource, "");
49 strcpy(lpsParams, "");
50
51 lpsPos = strstr(lpsUri, "://");
52 if (lpsPos != NULL)
53 lpsStr = &lpsPos[3];
54 else
55 lpsStr = lpsUri;
56
57 lpsPos = strstr(lpsStr, "/");
58 if (lpsPos != NULL) {
59 strncat(lpsHost, lpsPos, lpsPos - lpsStr);
60 lpsStr = &lpsPos[1];
61
62 lpsPos = strstr(lpsStr, "?");
63 if (lpsPos != NULL) {
64 strncat(lpsResource, lpsStr, lpsPos - lpsStr);
65 strcpy(lpsParams, &lpsPos[1]);
66 } else {
67 strcpy(lpsResource, lpsStr);
68 strcpy(lpsParams, "");
69 }
70
71 // Replace "/" with "\"
72 for (i = 0; i < strlen(lpsResource); i++) {
73 if (lpsResource[i] == '/')
74 lpsResource[i] = '\\';
75 }
76 }
77}
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
unsigned int UINT
Definition: ndis.h:50

Referenced by ProcessRequest().

Member Data Documentation

◆ bStop

BOOL CHttpClient::bStop
private

Definition at line 42 of file httpd.h.

Referenced by OnClose(), and SendFile().

◆ hFile

HANDLE CHttpClient::hFile
private

Definition at line 49 of file httpd.h.

◆ lpsBuffer

LPSTR CHttpClient::lpsBuffer
private

Definition at line 43 of file httpd.h.

Referenced by SendFile().

◆ nBufferSize

LONG CHttpClient::nBufferSize
private

Definition at line 44 of file httpd.h.

Referenced by SendFile().

◆ nFileSize

unsigned long long CHttpClient::nFileSize
private

Definition at line 48 of file httpd.h.

Referenced by SendFile().

◆ nTotalRead

unsigned long long CHttpClient::nTotalRead
private

Definition at line 46 of file httpd.h.

Referenced by SendFile().

◆ Parser

CHttpParser CHttpClient::Parser

Definition at line 35 of file httpd.h.

Referenced by OnRead(), and ProcessRequest().

◆ ThreadHandle

HANDLE CHttpClient::ThreadHandle

Definition at line 33 of file httpd.h.

◆ ThreadId

DWORD CHttpClient::ThreadId

Definition at line 34 of file httpd.h.


The documentation for this class was generated from the following files: