2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
15 #include "debugtools.h"
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_SOCKET_H
21 # include <sys/socket.h>
33 DEFAULT_DEBUG_CHANNEL(wininet
);
35 #define HTTPHEADER " HTTP/1.0"
36 #define HTTPHOSTHEADER "\r\nHost: "
37 #define MAXHOSTNAME 100
38 #define MAX_FIELD_VALUE_LEN 256
39 #define MAX_FIELD_LEN 256
42 #define HTTP_REFERER "Referer"
43 #define HTTP_ACCEPT "Accept"
45 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
46 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
47 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
48 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
49 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
50 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
51 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
54 BOOL
HTTP_OpenConnection(LPWININETHTTPREQA lpwhr
);
55 int HTTP_WriteDataToStream(LPWININETHTTPREQA lpwhr
,
56 void *Buffer
, int BytesToWrite
);
57 int HTTP_ReadDataFromStream(LPWININETHTTPREQA lpwhr
,
58 void *Buffer
, int BytesToRead
);
59 BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr
);
60 BOOL
HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr
, LPCSTR field
, LPCSTR value
, DWORD dwModifier
);
61 void HTTP_CloseConnection(LPWININETHTTPREQA lpwhr
);
62 BOOL
HTTP_InterpretHttpHeader(LPSTR buffer
, LPSTR field
, INT fieldlen
, LPSTR value
, INT valuelen
);
63 INT
HTTP_GetStdHeaderIndex(LPCSTR lpszField
);
64 INT
HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr
, LPHTTPHEADERA lpHdr
);
65 INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr
, LPCSTR lpszField
);
67 inline static LPSTR
HTTP_strdup( LPCSTR str
)
69 LPSTR ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
70 if (ret
) strcpy( ret
, str
);
74 /***********************************************************************
75 * HttpAddRequestHeadersA (WININET.68)
77 * Adds one or more HTTP header to the request handler
84 INTERNETAPI BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
85 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
90 CHAR value
[MAX_FIELD_VALUE_LEN
], field
[MAX_FIELD_LEN
];
91 BOOL bSuccess
= FALSE
;
92 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
94 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
96 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
100 buffer
= HTTP_strdup(lpszHeader
);
107 while (*lpszEnd
!= '\0')
109 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
114 if (*lpszEnd
== '\0')
119 if (HTTP_InterpretHttpHeader(lpszStart
, field
, MAX_FIELD_LEN
, value
, MAX_FIELD_VALUE_LEN
))
120 bSuccess
= HTTP_ProcessHeader(lpwhr
, field
, value
, dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
122 lpszStart
= lpszEnd
+ 2; /* Jump over \0\n */
126 HeapFree(GetProcessHeap(), 0, buffer
);
131 /***********************************************************************
132 * HttpOpenRequestA (WININET.72)
134 * Open a HTTP request handle
137 * HINTERNET a HTTP request handle on success
141 INTERNETAPI HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
142 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
143 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
144 DWORD dwFlags
, DWORD dwContext
)
146 LPWININETHTTPSESSIONA lpwhs
= (LPWININETHTTPSESSIONA
) hHttpSession
;
147 LPWININETAPPINFOA hIC
= NULL
;
151 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
153 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
157 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
159 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
161 WORKREQUEST workRequest
;
163 workRequest
.asyncall
= HTTPOPENREQUESTA
;
164 workRequest
.HFTPSESSION
= (DWORD
)hHttpSession
;
165 workRequest
.LPSZVERB
= (DWORD
)HTTP_strdup(lpszVerb
);
166 workRequest
.LPSZOBJECTNAME
= (DWORD
)HTTP_strdup(lpszObjectName
);
167 workRequest
.LPSZVERSION
= (DWORD
)HTTP_strdup(lpszVersion
);
168 workRequest
.LPSZREFERRER
= (DWORD
)HTTP_strdup(lpszReferrer
);
169 workRequest
.LPSZACCEPTTYPES
= (DWORD
)lpszAcceptTypes
;
170 workRequest
.DWFLAGS
= dwFlags
;
171 workRequest
.DWCONTEXT
= dwContext
;
173 return (HINTERNET
)INTERNET_AsyncCall(&workRequest
);
177 return HTTP_HttpOpenRequestA(hHttpSession
, lpszVerb
, lpszObjectName
,
178 lpszVersion
, lpszReferrer
, lpszAcceptTypes
, dwFlags
, dwContext
);
183 /***********************************************************************
184 * HTTP_HttpOpenRequestA (internal)
186 * Open a HTTP request handle
189 * HINTERNET a HTTP request handle on success
193 INTERNETAPI HINTERNET WINAPI
HTTP_HttpOpenRequestA(HINTERNET hHttpSession
,
194 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
195 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
196 DWORD dwFlags
, DWORD dwContext
)
198 LPWININETHTTPSESSIONA lpwhs
= (LPWININETHTTPSESSIONA
) hHttpSession
;
199 LPWININETAPPINFOA hIC
= NULL
;
200 LPWININETHTTPREQA lpwhr
;
204 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
206 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
210 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
212 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQA
));
215 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
216 return (HINTERNET
) NULL
;
219 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
220 lpwhr
->hdr
.lpwhparent
= hHttpSession
;
221 lpwhr
->hdr
.dwFlags
= dwFlags
;
222 lpwhr
->hdr
.dwContext
= dwContext
;
223 lpwhr
->nSocketFD
= INVALID_SOCKET
;
225 if (NULL
!= lpszObjectName
&& strlen(lpszObjectName
))
226 lpwhr
->lpszPath
= HTTP_strdup(lpszObjectName
);
228 if (NULL
!= lpszReferrer
&& strlen(lpszReferrer
))
229 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDHDR_FLAG_COALESCE
);
232 if (NULL
!= lpszAcceptTypes
&& strlen(*lpszAcceptTypes
))
233 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, *lpszAcceptTypes
, HTTP_ADDHDR_FLAG_COALESCE
);
235 if (NULL
== lpszVerb
)
236 lpwhr
->lpszVerb
= HTTP_strdup("GET");
237 else if (strlen(lpszVerb
))
238 lpwhr
->lpszVerb
= HTTP_strdup(lpszVerb
);
240 if (NULL
!= lpszReferrer
)
242 char buf
[MAXHOSTNAME
];
243 URL_COMPONENTSA UrlComponents
;
245 UrlComponents
.lpszExtraInfo
= NULL
;
246 UrlComponents
.lpszPassword
= NULL
;
247 UrlComponents
.lpszScheme
= NULL
;
248 UrlComponents
.lpszUrlPath
= NULL
;
249 UrlComponents
.lpszUserName
= NULL
;
250 UrlComponents
.lpszHostName
= buf
;
251 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
253 InternetCrackUrlA(lpszReferrer
, 0, 0, &UrlComponents
);
254 if (strlen(UrlComponents
.lpszHostName
))
255 lpwhr
->lpszHostName
= HTTP_strdup(UrlComponents
.lpszHostName
);
257 lpwhr
->lpszHostName
= HTTP_strdup(lpwhs
->lpszServerName
);
260 if (hIC
->lpfnStatusCB
)
262 INTERNET_ASYNC_RESULT iar
;
264 iar
.dwResult
= (DWORD
)lpwhr
;
265 iar
.dwError
= ERROR_SUCCESS
;
267 hIC
->lpfnStatusCB(hHttpSession
, dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
268 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
271 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
&& hIC
->lpfnStatusCB
)
273 INTERNET_ASYNC_RESULT iar
;
275 iar
.dwResult
= (DWORD
)lpwhr
;
276 iar
.dwError
= lpwhr
? ERROR_SUCCESS
: INTERNET_GetLastError();
277 hIC
->lpfnStatusCB(hHttpSession
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
278 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
281 return (HINTERNET
) lpwhr
;
285 /***********************************************************************
286 * HttpQueryInfoA (WININET.74)
288 * Queries for information about an HTTP request
295 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
296 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
298 LPHTTPHEADERA lphttpHdr
= NULL
;
299 BOOL bSuccess
= FALSE
;
300 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
302 TRACE("(0x%08lx)--> %ld\n", dwInfoLevel
, dwInfoLevel
);
304 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
306 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
310 /* Find requested header structure */
311 if ((dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
) == HTTP_QUERY_CUSTOM
)
313 INT index
= HTTP_GetCustomHeaderIndex(lpwhr
, (LPSTR
)lpBuffer
);
318 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
322 INT index
= dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
;
324 if (index
== HTTP_QUERY_RAW_HEADERS_CRLF
|| index
== HTTP_QUERY_RAW_HEADERS
)
326 INT i
, delim
, size
= 0, cnt
= 0;
328 delim
= index
== HTTP_QUERY_RAW_HEADERS_CRLF
? 2 : 1;
330 /* Calculate length of custom reuqest headers */
331 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
333 if ((~lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
) && lpwhr
->pCustHeaders
[i
].lpszField
&&
334 lpwhr
->pCustHeaders
[i
].lpszValue
)
336 size
+= strlen(lpwhr
->pCustHeaders
[i
].lpszField
) +
337 strlen(lpwhr
->pCustHeaders
[i
].lpszValue
) + delim
+ 2;
341 /* Calculate the length of stadard request headers */
342 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
344 if ((~lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
) && lpwhr
->StdHeaders
[i
].lpszField
&&
345 lpwhr
->StdHeaders
[i
].lpszValue
)
347 size
+= strlen(lpwhr
->StdHeaders
[i
].lpszField
) +
348 strlen(lpwhr
->StdHeaders
[i
].lpszValue
) + delim
+ 2;
354 if (size
+ 1 > *lpdwBufferLength
)
356 *lpdwBufferLength
= size
+ 1;
357 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
361 /* Append standard request heades */
362 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
364 if ((~lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
) &&
365 lpwhr
->StdHeaders
[i
].lpszField
&&
366 lpwhr
->StdHeaders
[i
].lpszValue
)
368 cnt
+= sprintf(lpBuffer
+ cnt
, "%s: %s%s", lpwhr
->StdHeaders
[i
].lpszField
, lpwhr
->StdHeaders
[i
].lpszValue
,
369 index
== HTTP_QUERY_RAW_HEADERS_CRLF
? "\r\n" : "\0");
373 /* Append custom request heades */
374 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
376 if ((~lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
) &&
377 lpwhr
->pCustHeaders
[i
].lpszField
&&
378 lpwhr
->pCustHeaders
[i
].lpszValue
)
380 cnt
+= sprintf(lpBuffer
+ cnt
, "%s: %s%s",
381 lpwhr
->pCustHeaders
[i
].lpszField
, lpwhr
->pCustHeaders
[i
].lpszValue
,
382 index
== HTTP_QUERY_RAW_HEADERS_CRLF
? "\r\n" : "\0");
386 strcpy(lpBuffer
+ cnt
, index
== HTTP_QUERY_RAW_HEADERS_CRLF
? "\r\n" : "");
388 *lpdwBufferLength
= cnt
+ delim
;
392 else if (index
>= 0 && index
<= HTTP_QUERY_MAX
&& lpwhr
->StdHeaders
[index
].lpszValue
)
394 lphttpHdr
= &lpwhr
->StdHeaders
[index
];
400 /* Ensure header satisifies requested attributes */
401 if ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
402 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
))
405 /* coalesce value to reuqested type */
406 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
)
408 *(int *)lpBuffer
= atoi(lphttpHdr
->lpszValue
);
411 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
)
417 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
419 tmpTM
= *gmtime(&tmpTime
);
420 STHook
= (SYSTEMTIME
*) lpBuffer
;
424 STHook
->wDay
= tmpTM
.tm_mday
;
425 STHook
->wHour
= tmpTM
.tm_hour
;
426 STHook
->wMilliseconds
= 0;
427 STHook
->wMinute
= tmpTM
.tm_min
;
428 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
429 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
430 STHook
->wSecond
= tmpTM
.tm_sec
;
431 STHook
->wYear
= tmpTM
.tm_year
;
435 else if (dwInfoLevel
& HTTP_QUERY_FLAG_COALESCE
)
437 if (*lpdwIndex
>= lphttpHdr
->wCount
)
439 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
443 //! Copy strncpy(lpBuffer, lphttpHdr[*lpdwIndex], len);
449 INT len
= strlen(lphttpHdr
->lpszValue
);
451 if (len
+ 1 > *lpdwBufferLength
)
453 *lpdwBufferLength
= len
+ 1;
454 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
458 strncpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
459 *lpdwBufferLength
= len
;
464 TRACE("%d <--\n", bSuccess
);
469 /***********************************************************************
470 * HttpSendRequestExA (WININET)
472 * Sends the specified request to the HTTP server and allows chunked
475 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
476 LPINTERNET_BUFFERSA lpBuffersIn
,
477 LPINTERNET_BUFFERSA lpBuffersOut
,
478 DWORD dwFlags
, DWORD dwContext
)
480 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest
, lpBuffersIn
,
481 lpBuffersOut
, dwFlags
, dwContext
);
485 /***********************************************************************
486 * HttpSendRequestA (WININET.76)
488 * Sends the specified request to the HTTP server
495 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
496 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
498 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
499 LPWININETHTTPSESSIONA lpwhs
= NULL
;
500 LPWININETAPPINFOA hIC
= NULL
;
502 TRACE("0x%08lx\n", (unsigned long)hHttpRequest
);
504 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
506 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
510 lpwhs
= (LPWININETHTTPSESSIONA
) lpwhr
->hdr
.lpwhparent
;
511 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
513 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
517 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
518 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
520 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
524 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
526 WORKREQUEST workRequest
;
528 workRequest
.asyncall
= HTTPSENDREQUESTA
;
529 workRequest
.HFTPSESSION
= (DWORD
)hHttpRequest
;
530 workRequest
.LPSZHEADER
= (DWORD
)HTTP_strdup(lpszHeaders
);
531 workRequest
.DWHEADERLENGTH
= dwHeaderLength
;
532 workRequest
.LPOPTIONAL
= (DWORD
)lpOptional
;
533 workRequest
.DWOPTIONALLENGTH
= dwOptionalLength
;
535 return INTERNET_AsyncCall(&workRequest
);
539 return HTTP_HttpSendRequestA(hHttpRequest
, lpszHeaders
,
540 dwHeaderLength
, lpOptional
, dwOptionalLength
);
545 /***********************************************************************
546 * HTTP_HttpSendRequestA (internal)
548 * Sends the specified request to the HTTP server
555 BOOL WINAPI
HTTP_HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
556 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
560 BOOL bSuccess
= FALSE
;
561 LPSTR requestString
= NULL
;
562 INT requestStringLen
;
563 INT headerLength
= 0;
564 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
565 LPWININETHTTPSESSIONA lpwhs
= NULL
;
566 LPWININETAPPINFOA hIC
= NULL
;
568 TRACE("0x%08lx\n", (ULONG
)hHttpRequest
);
570 /* Verify our tree of internet handles */
571 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
573 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
577 lpwhs
= (LPWININETHTTPSESSIONA
) lpwhr
->hdr
.lpwhparent
;
578 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
580 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
584 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
585 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
587 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
591 /* Clear any error information */
592 INTERNET_SetLastError(0);
594 /* We must have a verb */
595 if (NULL
== lpwhr
->lpszVerb
)
600 /* If we don't have a path we set it to root */
601 if (NULL
== lpwhr
->lpszPath
)
602 lpwhr
->lpszPath
= HTTP_strdup("/");
604 /* Calculate length of request string */
606 strlen(lpwhr
->lpszVerb
) +
607 strlen(lpwhr
->lpszPath
) +
608 (lpwhr
->lpszHostName
? (strlen(HTTPHOSTHEADER
) + strlen(lpwhr
->lpszHostName
)) : 0) +
612 /* Add length of passed headers */
615 headerLength
= -1 == dwHeaderLength
? strlen(lpszHeaders
) : dwHeaderLength
;
616 requestStringLen
+= headerLength
+ 2; /* \r\n */
619 /* Calculate length of custom request headers */
620 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
622 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
624 requestStringLen
+= strlen(lpwhr
->pCustHeaders
[i
].lpszField
) +
625 strlen(lpwhr
->pCustHeaders
[i
].lpszValue
) + 4; /*: \r\n */
629 /* Calculate the length of standard request headers */
630 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
632 if (lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
)
634 requestStringLen
+= strlen(lpwhr
->StdHeaders
[i
].lpszField
) +
635 strlen(lpwhr
->StdHeaders
[i
].lpszValue
) + 4; /*: \r\n */
639 /* Allocate string to hold entire request */
640 requestString
= HeapAlloc(GetProcessHeap(), 0, requestStringLen
+ 1);
641 if (NULL
== requestString
)
643 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
647 /* Build request string */
648 cnt
= sprintf(requestString
, "%s %s%s%s",
651 lpwhr
->lpszHostName
? (HTTPHEADER HTTPHOSTHEADER
) : HTTPHEADER
,
652 lpwhr
->lpszHostName
? lpwhr
->lpszHostName
: "");
654 /* Append standard request headers */
655 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
657 if (lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
)
659 cnt
+= sprintf(requestString
+ cnt
, "\r\n%s: %s",
660 lpwhr
->StdHeaders
[i
].lpszField
, lpwhr
->StdHeaders
[i
].lpszValue
);
664 /* Append custom request heades */
665 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
667 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
669 cnt
+= sprintf(requestString
+ cnt
, "\r\n%s: %s",
670 lpwhr
->pCustHeaders
[i
].lpszField
, lpwhr
->pCustHeaders
[i
].lpszValue
);
674 /* Append passed request headers */
677 strcpy(requestString
+ cnt
, "\r\n");
679 strcpy(requestString
+ cnt
, lpszHeaders
);
683 /* Set termination string for request */
684 strcpy(requestString
+ cnt
, "\r\n\r\n");
686 if (hIC
->lpfnStatusCB
)
687 hIC
->lpfnStatusCB(hHttpRequest
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
689 TRACE("(%s) len(%d)\n", requestString
, requestStringLen
);
690 /* Send the request and store the results */
691 if (!HTTP_OpenConnection(lpwhr
))
694 cnt
= INTERNET_WriteDataToStream(lpwhr
->nSocketFD
, requestString
, requestStringLen
);
699 if (HTTP_GetResponseHeaders(lpwhr
))
705 HeapFree(GetProcessHeap(), 0, requestString
);
707 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
&& hIC
->lpfnStatusCB
)
709 INTERNET_ASYNC_RESULT iar
;
711 iar
.dwResult
= (DWORD
)bSuccess
;
712 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
713 hIC
->lpfnStatusCB(hHttpRequest
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
714 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
722 /***********************************************************************
723 * HTTP_Connect (internal)
725 * Create http session handle
728 * HINTERNET a session handle on success
732 HINTERNET
HTTP_Connect(HINTERNET hInternet
, LPCSTR lpszServerName
,
733 INTERNET_PORT nServerPort
, LPCSTR lpszUserName
,
734 LPCSTR lpszPassword
, DWORD dwFlags
, DWORD dwContext
)
736 BOOL bSuccess
= FALSE
;
737 LPWININETAPPINFOA hIC
= NULL
;
738 LPWININETHTTPSESSIONA lpwhs
= NULL
;
742 if (((LPWININETHANDLEHEADER
)hInternet
)->htype
!= WH_HINIT
)
745 hIC
= (LPWININETAPPINFOA
) hInternet
;
747 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONA
));
750 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
754 if (hIC
->lpfnStatusCB
)
755 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
756 (LPVOID
)lpszServerName
, strlen(lpszServerName
));
758 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
759 nServerPort
= INTERNET_DEFAULT_HTTP_PORT
;
761 if (!GetAddress(lpszServerName
, nServerPort
, &lpwhs
->phostent
, &lpwhs
->socketAddress
))
763 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
767 if (hIC
->lpfnStatusCB
)
768 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
769 (LPVOID
)lpszServerName
, strlen(lpszServerName
));
771 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
772 lpwhs
->hdr
.lpwhparent
= (LPWININETHANDLEHEADER
)hInternet
;
773 lpwhs
->hdr
.dwFlags
= dwFlags
;
774 lpwhs
->hdr
.dwContext
= dwContext
;
775 if (NULL
!= lpszServerName
)
776 lpwhs
->lpszServerName
= HTTP_strdup(lpszServerName
);
777 if (NULL
!= lpszUserName
)
778 lpwhs
->lpszUserName
= HTTP_strdup(lpszUserName
);
779 lpwhs
->nServerPort
= nServerPort
;
781 if (hIC
->lpfnStatusCB
)
783 INTERNET_ASYNC_RESULT iar
;
785 iar
.dwResult
= (DWORD
)lpwhs
;
786 iar
.dwError
= ERROR_SUCCESS
;
788 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
789 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
795 if (!bSuccess
&& lpwhs
)
797 HeapFree(GetProcessHeap(), 0, lpwhs
);
801 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
&& hIC
->lpfnStatusCB
)
803 INTERNET_ASYNC_RESULT iar
;
805 iar
.dwResult
= (DWORD
)lpwhs
;
806 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
807 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
808 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
811 return (HINTERNET
)lpwhs
;
815 /***********************************************************************
816 * HTTP_OpenConnection (internal)
818 * Connect to a web server
825 BOOL
HTTP_OpenConnection(LPWININETHTTPREQA lpwhr
)
827 BOOL bSuccess
= FALSE
;
829 LPWININETHTTPSESSIONA lpwhs
;
833 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
835 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
839 lpwhs
= (LPWININETHTTPSESSIONA
)lpwhr
->hdr
.lpwhparent
;
841 lpwhr
->nSocketFD
= socket(lpwhs
->phostent
->h_addrtype
,SOCK_STREAM
,0);
842 if (INVALID_SOCKET
== lpwhr
->nSocketFD
)
844 WARN("Socket creation failed\n");
848 result
= connect(lpwhr
->nSocketFD
, (struct sockaddr
*)&lpwhs
->socketAddress
,
849 sizeof(lpwhs
->socketAddress
));
851 if (SOCKET_ERROR
== result
)
853 WARN("Unable to connect to host (%s)\n", strerror(errno
));
860 TRACE(": %d\n", bSuccess
);
865 /***********************************************************************
866 * HTTP_GetResponseHeaders (internal)
868 * Read server response
875 BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr
)
878 CHAR buffer
[MAX_REPLY_LEN
];
879 DWORD buflen
= MAX_REPLY_LEN
;
880 BOOL bSuccess
= FALSE
;
881 CHAR value
[MAX_FIELD_VALUE_LEN
], field
[MAX_FIELD_LEN
];
885 if (INVALID_SOCKET
== lpwhr
->nSocketFD
)
889 * We should first receive 'HTTP/1.x nnn' where nnn is the status code.
891 if (!INTERNET_GetNextLine(lpwhr
->nSocketFD
, buffer
, &buflen
))
894 if (strncmp(buffer
, "HTTP", 4) != 0)
898 HTTP_ProcessHeader(lpwhr
, "Status", buffer
+9, (HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
));
900 /* Parse each response line */
903 buflen
= MAX_REPLY_LEN
;
904 if (INTERNET_GetNextLine(lpwhr
->nSocketFD
, buffer
, &buflen
))
906 if (!HTTP_InterpretHttpHeader(buffer
, field
, MAX_FIELD_LEN
, value
, MAX_FIELD_VALUE_LEN
))
909 HTTP_ProcessHeader(lpwhr
, field
, value
, (HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
));
927 /***********************************************************************
928 * HTTP_InterpretHttpHeader (internal)
930 * Parse server response
937 INT
stripSpaces(LPCSTR lpszSrc
, LPSTR lpszStart
, INT
*len
)
944 while (*lpszSrc
== ' ' && *lpszSrc
!= '\0')
948 while(*lpsztmp
!= '\0')
951 srclen
= lpsztmp
- lpszSrc
+ 1;
956 *len
= min(*len
, srclen
);
957 strncpy(lpszStart
, lpszSrc
, *len
);
958 lpszStart
[*len
] = '\0';
964 BOOL
HTTP_InterpretHttpHeader(LPSTR buffer
, LPSTR field
, INT fieldlen
, LPSTR value
, INT valuelen
)
967 BOOL bSuccess
= FALSE
;
974 pd
= strchr(buffer
, ':');
978 if (stripSpaces(buffer
, field
, &fieldlen
) > 0)
980 if (stripSpaces(pd
+1, value
, &valuelen
) > 0)
985 TRACE("%d: field(%s) Value(%s)\n", bSuccess
, field
, value
);
990 /***********************************************************************
991 * HTTP_GetStdHeaderIndex (internal)
993 * Lookup field index in standard http header array
995 * FIXME: This should be stuffed into a hash table
997 INT
HTTP_GetStdHeaderIndex(LPCSTR lpszField
)
1001 if (!strcasecmp(lpszField
, "Content-Length"))
1002 index
= HTTP_QUERY_CONTENT_LENGTH
;
1003 else if (!strcasecmp(lpszField
,"Status"))
1004 index
= HTTP_QUERY_STATUS_CODE
;
1005 else if (!strcasecmp(lpszField
,"Content-Type"))
1006 index
= HTTP_QUERY_CONTENT_TYPE
;
1007 else if (!strcasecmp(lpszField
,"Last-Modified"))
1008 index
= HTTP_QUERY_LAST_MODIFIED
;
1009 else if (!strcasecmp(lpszField
,"Location"))
1010 index
= HTTP_QUERY_LOCATION
;
1011 else if (!strcasecmp(lpszField
,"Accept"))
1012 index
= HTTP_QUERY_ACCEPT
;
1013 else if (!strcasecmp(lpszField
,"Referer"))
1014 index
= HTTP_QUERY_REFERER
;
1015 else if (!strcasecmp(lpszField
,"Content-Transfer-Encoding"))
1016 index
= HTTP_QUERY_CONTENT_TRANSFER_ENCODING
;
1017 else if (!strcasecmp(lpszField
,"Date"))
1018 index
= HTTP_QUERY_DATE
;
1019 else if (!strcasecmp(lpszField
,"Server"))
1020 index
= HTTP_QUERY_SERVER
;
1021 else if (!strcasecmp(lpszField
,"Connection"))
1022 index
= HTTP_QUERY_CONNECTION
;
1023 else if (!strcasecmp(lpszField
,"ETag"))
1024 index
= HTTP_QUERY_ETAG
;
1025 else if (!strcasecmp(lpszField
,"Accept-Ranges"))
1026 index
= HTTP_QUERY_ACCEPT_RANGES
;
1027 else if (!strcasecmp(lpszField
,"Expires"))
1028 index
= HTTP_QUERY_EXPIRES
;
1029 else if (!strcasecmp(lpszField
,"Mime-Version"))
1030 index
= HTTP_QUERY_MIME_VERSION
;
1033 FIXME("Couldn't find %s in standard header table\n", lpszField
);
1040 /***********************************************************************
1041 * HTTP_ProcessHeader (internal)
1043 * Stuff header into header tables according to <dwModifier>
1047 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1049 BOOL
HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr
, LPCSTR field
, LPCSTR value
, DWORD dwModifier
)
1051 LPHTTPHEADERA lphttpHdr
= NULL
;
1052 BOOL bSuccess
= FALSE
;
1055 TRACE("%s:%s - 0x%08x\n", field
, value
, (unsigned int)dwModifier
);
1057 /* Adjust modifier flags */
1058 if (dwModifier
& COALESCEFLASG
)
1059 dwModifier
|= HTTP_ADDHDR_FLAG_ADD
;
1061 /* Try to get index into standard header array */
1062 index
= HTTP_GetStdHeaderIndex(field
);
1065 lphttpHdr
= &lpwhr
->StdHeaders
[index
];
1067 else /* Find or create new custom header */
1069 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
);
1072 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
1076 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
1082 hdr
.lpszField
= (LPSTR
)field
;
1083 hdr
.lpszValue
= (LPSTR
)value
;
1084 hdr
.wFlags
= hdr
.wCount
= 0;
1086 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
1087 hdr
.wFlags
|= HDR_ISREQUEST
;
1089 index
= HTTP_InsertCustomHeader(lpwhr
, &hdr
);
1094 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
1095 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
1097 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
1099 if (!lphttpHdr
->lpszValue
&& (dwModifier
& (HTTP_ADDHDR_FLAG_ADD
|HTTP_ADDHDR_FLAG_ADD_IF_NEW
)))
1103 if (!lpwhr
->StdHeaders
[index
].lpszField
)
1105 lphttpHdr
->lpszField
= HTTP_strdup(field
);
1107 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
1108 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
1111 slen
= strlen(value
) + 1;
1112 lphttpHdr
->lpszValue
= HeapAlloc(GetProcessHeap(), 0, slen
);
1113 if (lphttpHdr
->lpszValue
)
1115 memcpy(lphttpHdr
->lpszValue
, value
, slen
);
1120 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1123 else if (lphttpHdr
->lpszValue
)
1125 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
1130 len
= strlen(value
);
1134 //! if custom header delete from array
1135 HeapFree(GetProcessHeap(), 0, lphttpHdr
->lpszValue
);
1136 lphttpHdr
->lpszValue
= NULL
;
1141 lpsztmp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lphttpHdr
->lpszValue
, len
+1);
1144 lphttpHdr
->lpszValue
= lpsztmp
;
1145 strcpy(lpsztmp
, value
);
1150 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1154 else if (dwModifier
& COALESCEFLASG
)
1159 INT origlen
= strlen(lphttpHdr
->lpszValue
);
1160 INT valuelen
= strlen(value
);
1162 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
1165 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
1167 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
1170 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
1173 len
= origlen
+ valuelen
+ (ch
> 0) ? 1 : 0;
1175 lpsztmp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lphttpHdr
->lpszValue
, len
+1);
1178 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
1181 lphttpHdr
->lpszValue
[origlen
] = ch
;
1185 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
);
1186 lphttpHdr
->lpszValue
[len
] = '\0';
1191 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1200 /***********************************************************************
1201 * HTTP_CloseConnection (internal)
1203 * Close socket connection
1206 VOID
HTTP_CloseConnection(LPWININETHTTPREQA lpwhr
)
1208 if (lpwhr
->nSocketFD
!= INVALID_SOCKET
)
1210 close(lpwhr
->nSocketFD
);
1211 lpwhr
->nSocketFD
= INVALID_SOCKET
;
1216 /***********************************************************************
1217 * HTTP_CloseHTTPRequestHandle (internal)
1219 * Deallocate request handle
1222 void HTTP_CloseHTTPRequestHandle(LPWININETHTTPREQA lpwhr
)
1228 if (lpwhr
->nSocketFD
!= INVALID_SOCKET
)
1229 HTTP_CloseConnection(lpwhr
);
1231 if (lpwhr
->lpszPath
)
1232 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
1233 if (lpwhr
->lpszVerb
)
1234 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
1235 if (lpwhr
->lpszHostName
)
1236 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszHostName
);
1238 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
1240 if (lpwhr
->StdHeaders
[i
].lpszField
)
1241 HeapFree(GetProcessHeap(), 0, lpwhr
->StdHeaders
[i
].lpszField
);
1242 if (lpwhr
->StdHeaders
[i
].lpszValue
)
1243 HeapFree(GetProcessHeap(), 0, lpwhr
->StdHeaders
[i
].lpszValue
);
1246 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
1248 if (lpwhr
->pCustHeaders
[i
].lpszField
)
1249 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszField
);
1250 if (lpwhr
->pCustHeaders
[i
].lpszValue
)
1251 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszValue
);
1254 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
);
1255 HeapFree(GetProcessHeap(), 0, lpwhr
);
1259 /***********************************************************************
1260 * HTTP_CloseHTTPSessionHandle (internal)
1262 * Deallocate session handle
1265 void HTTP_CloseHTTPSessionHandle(LPWININETHTTPSESSIONA lpwhs
)
1269 if (lpwhs
->lpszServerName
)
1270 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
1271 if (lpwhs
->lpszUserName
)
1272 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
1273 HeapFree(GetProcessHeap(), 0, lpwhs
);
1277 /***********************************************************************
1278 * HTTP_GetCustomHeaderIndex (internal)
1280 * Return index of custom header from header array
1283 INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr
, LPCSTR lpszField
)
1287 TRACE("%s\n", lpszField
);
1289 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
1291 if (!strcasecmp(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
1296 if (index
>= lpwhr
->nCustHeaders
)
1299 TRACE("Return: %d\n", index
);
1304 /***********************************************************************
1305 * HTTP_InsertCustomHeader (internal)
1307 * Insert header into array
1310 INT
HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr
, LPHTTPHEADERA lpHdr
)
1313 LPHTTPHEADERA lph
= NULL
;
1315 TRACE("%s: %s\n", lpHdr
->lpszField
, lpHdr
->lpszValue
);
1316 count
= lpwhr
->nCustHeaders
+ 1;
1318 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERA
) * count
);
1320 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERA
) * count
);
1324 lpwhr
->pCustHeaders
= lph
;
1325 lpwhr
->pCustHeaders
[count
-1].lpszField
= HTTP_strdup(lpHdr
->lpszField
);
1326 lpwhr
->pCustHeaders
[count
-1].lpszValue
= HTTP_strdup(lpHdr
->lpszValue
);
1327 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
1328 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
1329 lpwhr
->nCustHeaders
++;
1333 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1337 TRACE("%d <--\n", count
-1);
1342 /***********************************************************************
1343 * HTTP_DeleteCustomHeader (internal)
1345 * Delete header from array
1348 BOOL
HTTP_DeleteCustomHeader(INT index
)