2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
14 #include "debugtools.h"
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_SOCKET_H
21 # include <sys/socket.h>
31 DEFAULT_DEBUG_CHANNEL(wininet
)
33 #define HTTPHEADER " HTTP/1.0"
34 #define HTTPHOSTHEADER "\r\nHost: "
35 #define MAXHOSTNAME 100
36 #define MAX_FIELD_VALUE_LEN 256
37 #define MAX_FIELD_LEN 256
40 #define HTTP_REFERER "Referer"
41 #define HTTP_ACCEPT "Accept"
43 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
44 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
45 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
46 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
47 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
48 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
49 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
52 BOOL
HTTP_OpenConnection(LPWININETHTTPREQA lpwhr
);
53 int HTTP_WriteDataToStream(LPWININETHTTPREQA lpwhr
,
54 void *Buffer
, int BytesToWrite
);
55 int HTTP_ReadDataFromStream(LPWININETHTTPREQA lpwhr
,
56 void *Buffer
, int BytesToRead
);
57 BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr
);
58 BOOL
HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr
, LPCSTR field
, LPCSTR value
, DWORD dwModifier
);
59 void HTTP_CloseConnection(LPWININETHTTPREQA lpwhr
);
60 BOOL
HTTP_InterpretHttpHeader(LPSTR buffer
, LPSTR field
, INT fieldlen
, LPSTR value
, INT valuelen
);
61 INT
HTTP_GetStdHeaderIndex(LPCSTR lpszField
);
62 INT
HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr
, LPHTTPHEADERA lpHdr
);
63 INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr
, LPCSTR lpszField
);
65 /***********************************************************************
66 * HttpAddRequestHeadersA (WININET.68)
68 * Adds one or more HTTP header to the request handler
75 INTERNETAPI BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
76 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
81 CHAR value
[MAX_FIELD_VALUE_LEN
], field
[MAX_FIELD_LEN
];
82 BOOL bSuccess
= FALSE
;
83 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
85 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
87 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
91 buffer
= HEAP_strdupA(GetProcessHeap(), 0, lpszHeader
);
98 while (*lpszEnd
!= '\0')
100 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
105 if (*lpszEnd
== '\0')
110 if (HTTP_InterpretHttpHeader(lpszStart
, field
, MAX_FIELD_LEN
, value
, MAX_FIELD_VALUE_LEN
))
111 bSuccess
= HTTP_ProcessHeader(lpwhr
, field
, value
, dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
113 lpszStart
= lpszEnd
+ 2; /* Jump over \0\n */
117 HeapFree(GetProcessHeap(), 0, buffer
);
122 /***********************************************************************
123 * HttpOpenRequestA (WININET.72)
125 * Open a HTTP request handle
128 * HINTERNET a HTTP request handle on success
132 INTERNETAPI HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
133 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
134 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
135 DWORD dwFlags
, DWORD dwContext
)
137 LPWININETHTTPSESSIONA lpwhs
= (LPWININETHTTPSESSIONA
) hHttpSession
;
138 LPWININETAPPINFOA hIC
= NULL
;
142 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
144 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
148 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
150 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
152 WORKREQUEST workRequest
;
154 workRequest
.asyncall
= HTTPOPENREQUESTA
;
155 workRequest
.HFTPSESSION
= (DWORD
)hHttpSession
;
156 workRequest
.LPSZVERB
= (DWORD
)HEAP_strdupA(GetProcessHeap(), 0, lpszVerb
);
157 workRequest
.LPSZOBJECTNAME
= (DWORD
)HEAP_strdupA(GetProcessHeap(), 0, lpszObjectName
);
158 workRequest
.LPSZVERSION
= (DWORD
)HEAP_strdupA(GetProcessHeap(), 0, lpszVersion
);
159 workRequest
.LPSZREFERRER
= (DWORD
)HEAP_strdupA(GetProcessHeap(), 0, lpszReferrer
);
160 workRequest
.LPSZACCEPTTYPES
= (DWORD
)lpszAcceptTypes
;
161 workRequest
.DWFLAGS
= dwFlags
;
162 workRequest
.DWCONTEXT
= dwContext
;
164 return (HINTERNET
)INTERNET_AsyncCall(&workRequest
);
168 return HTTP_HttpOpenRequestA(hHttpSession
, lpszVerb
, lpszObjectName
,
169 lpszVersion
, lpszReferrer
, lpszAcceptTypes
, dwFlags
, dwContext
);
174 /***********************************************************************
175 * HTTP_HttpOpenRequestA (internal)
177 * Open a HTTP request handle
180 * HINTERNET a HTTP request handle on success
184 INTERNETAPI HINTERNET WINAPI
HTTP_HttpOpenRequestA(HINTERNET hHttpSession
,
185 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
186 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
187 DWORD dwFlags
, DWORD dwContext
)
189 LPWININETHTTPSESSIONA lpwhs
= (LPWININETHTTPSESSIONA
) hHttpSession
;
190 LPWININETAPPINFOA hIC
= NULL
;
191 LPWININETHTTPREQA lpwhr
;
195 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
197 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
201 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
203 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQA
));
206 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
207 return (HINTERNET
) NULL
;
210 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
211 lpwhr
->hdr
.lpwhparent
= hHttpSession
;
212 lpwhr
->hdr
.dwFlags
= dwFlags
;
213 lpwhr
->hdr
.dwContext
= dwContext
;
215 if (NULL
!= lpszObjectName
&& strlen(lpszObjectName
))
216 lpwhr
->lpszPath
= HEAP_strdupA(GetProcessHeap(), 0, lpszObjectName
);
218 if (NULL
!= lpszReferrer
&& strlen(lpszReferrer
))
219 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDHDR_FLAG_COALESCE
);
222 if (NULL
!= lpszAcceptTypes
&& strlen(*lpszAcceptTypes
))
223 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, *lpszAcceptTypes
, HTTP_ADDHDR_FLAG_COALESCE
);
225 if (NULL
== lpszVerb
)
226 lpwhr
->lpszVerb
= HEAP_strdupA(GetProcessHeap(), 0, "GET");
227 else if (strlen(lpszVerb
))
228 lpwhr
->lpszVerb
= HEAP_strdupA(GetProcessHeap(), 0, lpszVerb
);
230 if (NULL
!= lpszReferrer
)
232 char buf
[MAXHOSTNAME
];
233 URL_COMPONENTSA UrlComponents
;
235 UrlComponents
.lpszExtraInfo
= NULL
;
236 UrlComponents
.lpszPassword
= NULL
;
237 UrlComponents
.lpszScheme
= NULL
;
238 UrlComponents
.lpszUrlPath
= NULL
;
239 UrlComponents
.lpszUserName
= NULL
;
240 UrlComponents
.lpszHostName
= buf
;
241 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
243 InternetCrackUrlA(lpszReferrer
, 0, 0, &UrlComponents
);
244 if (strlen(UrlComponents
.lpszHostName
))
245 lpwhr
->lpszHostName
= HEAP_strdupA(GetProcessHeap(), 0, UrlComponents
.lpszHostName
);
248 if (hIC
->lpfnStatusCB
)
250 INTERNET_ASYNC_RESULT iar
;
252 iar
.dwResult
= (DWORD
)lpwhr
;
253 iar
.dwError
= ERROR_SUCCESS
;
255 hIC
->lpfnStatusCB(hHttpSession
, dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
256 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
259 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
&& hIC
->lpfnStatusCB
)
261 INTERNET_ASYNC_RESULT iar
;
263 iar
.dwResult
= (DWORD
)lpwhr
;
264 iar
.dwError
= lpwhr
? ERROR_SUCCESS
: INTERNET_GetLastError();
265 hIC
->lpfnStatusCB(hHttpSession
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
266 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
269 return (HINTERNET
) lpwhr
;
273 /***********************************************************************
274 * HttpQueryInfoA (WININET.74)
276 * Queries for information about an HTTP request
283 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
284 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
286 LPHTTPHEADERA lphttpHdr
= NULL
;
287 BOOL bSuccess
= FALSE
;
288 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
290 TRACE("(0x%08lx)--> %ld\n", dwInfoLevel
, dwInfoLevel
);
292 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
294 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
298 /* Find requested header structure */
299 if ((dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
) == HTTP_QUERY_CUSTOM
)
301 INT index
= HTTP_GetCustomHeaderIndex(lpwhr
, (LPSTR
)lpBuffer
);
306 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
310 INT index
= dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
;
312 if (index
== HTTP_QUERY_RAW_HEADERS_CRLF
|| index
== HTTP_QUERY_RAW_HEADERS
)
314 INT i
, delim
, size
= 0, cnt
= 0;
316 delim
= index
== HTTP_QUERY_RAW_HEADERS_CRLF
? 2 : 1;
318 /* Calculate length of custom reuqest headers */
319 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
321 if ((~lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
) && lpwhr
->pCustHeaders
[i
].lpszField
&&
322 lpwhr
->pCustHeaders
[i
].lpszValue
)
324 size
+= strlen(lpwhr
->pCustHeaders
[i
].lpszField
) +
325 strlen(lpwhr
->pCustHeaders
[i
].lpszValue
) + delim
+ 2;
329 /* Calculate the length of stadard request headers */
330 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
332 if ((~lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
) && lpwhr
->StdHeaders
[i
].lpszField
&&
333 lpwhr
->StdHeaders
[i
].lpszValue
)
335 size
+= strlen(lpwhr
->StdHeaders
[i
].lpszField
) +
336 strlen(lpwhr
->StdHeaders
[i
].lpszValue
) + delim
+ 2;
342 if (size
+ 1 > *lpdwBufferLength
)
344 *lpdwBufferLength
= size
+ 1;
345 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
349 /* Append standard request heades */
350 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
352 if ((~lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
) &&
353 lpwhr
->StdHeaders
[i
].lpszField
&&
354 lpwhr
->StdHeaders
[i
].lpszValue
)
356 cnt
+= sprintf(lpBuffer
+ cnt
, "%s: %s%s", lpwhr
->StdHeaders
[i
].lpszField
, lpwhr
->StdHeaders
[i
].lpszValue
,
357 index
== HTTP_QUERY_RAW_HEADERS_CRLF
? "\r\n" : "\0");
361 /* Append custom request heades */
362 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
364 if ((~lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
) &&
365 lpwhr
->pCustHeaders
[i
].lpszField
&&
366 lpwhr
->pCustHeaders
[i
].lpszValue
)
368 cnt
+= sprintf(lpBuffer
+ cnt
, "%s: %s%s",
369 lpwhr
->pCustHeaders
[i
].lpszField
, lpwhr
->pCustHeaders
[i
].lpszValue
,
370 index
== HTTP_QUERY_RAW_HEADERS_CRLF
? "\r\n" : "\0");
374 strcpy(lpBuffer
+ cnt
, index
== HTTP_QUERY_RAW_HEADERS_CRLF
? "\r\n" : "");
376 *lpdwBufferLength
= cnt
+ delim
;
380 else if (index
>= 0 && index
<= HTTP_QUERY_MAX
&& lpwhr
->StdHeaders
[index
].lpszValue
)
382 lphttpHdr
= &lpwhr
->StdHeaders
[index
];
388 /* Ensure header satisifies requested attributes */
389 if ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
390 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
))
393 /* coalesce value to reuqested type */
394 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
)
396 *(int *)lpBuffer
= atoi(lphttpHdr
->lpszValue
);
399 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
)
405 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
407 tmpTM
= *gmtime(&tmpTime
);
408 STHook
= (SYSTEMTIME
*) lpBuffer
;
412 STHook
->wDay
= tmpTM
.tm_mday
;
413 STHook
->wHour
= tmpTM
.tm_hour
;
414 STHook
->wMilliseconds
= 0;
415 STHook
->wMinute
= tmpTM
.tm_min
;
416 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
417 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
418 STHook
->wSecond
= tmpTM
.tm_sec
;
419 STHook
->wYear
= tmpTM
.tm_year
;
423 else if (dwInfoLevel
& HTTP_QUERY_FLAG_COALESCE
)
425 if (*lpdwIndex
>= lphttpHdr
->wCount
)
427 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
431 //! Copy strncpy(lpBuffer, lphttpHdr[*lpdwIndex], len);
437 INT len
= strlen(lphttpHdr
->lpszValue
);
439 if (len
+ 1 > *lpdwBufferLength
)
441 *lpdwBufferLength
= len
+ 1;
442 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
446 strncpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
447 *lpdwBufferLength
= len
;
452 TRACE("%d <--\n", bSuccess
);
456 /***********************************************************************
457 * HttpSendRequestA (WININET.76)
459 * Sends the specified request to the HTTP server
466 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
467 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
469 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
470 LPWININETHTTPSESSIONA lpwhs
= NULL
;
471 LPWININETAPPINFOA hIC
= NULL
;
473 TRACE("0x%08lx\n", (unsigned long)hHttpRequest
);
475 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
477 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
481 lpwhs
= (LPWININETHTTPSESSIONA
) lpwhr
->hdr
.lpwhparent
;
482 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
484 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
488 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
489 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
491 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
495 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
497 WORKREQUEST workRequest
;
499 workRequest
.asyncall
= HTTPSENDREQUESTA
;
500 workRequest
.HFTPSESSION
= (DWORD
)hHttpRequest
;
501 workRequest
.LPSZHEADER
= (DWORD
)HEAP_strdupA(GetProcessHeap(), 0, lpszHeaders
);
502 workRequest
.DWHEADERLENGTH
= dwHeaderLength
;
503 workRequest
.LPOPTIONAL
= (DWORD
)lpOptional
;
504 workRequest
.DWOPTIONALLENGTH
= dwOptionalLength
;
506 return INTERNET_AsyncCall(&workRequest
);
510 return HTTP_HttpSendRequestA(hHttpRequest
, lpszHeaders
,
511 dwHeaderLength
, lpOptional
, dwOptionalLength
);
516 /***********************************************************************
517 * HTTP_HttpSendRequestA (internal)
519 * Sends the specified request to the HTTP server
526 BOOL WINAPI
HTTP_HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
527 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
531 BOOL bSuccess
= FALSE
;
532 LPSTR requestString
= NULL
;
533 INT requestStringLen
;
534 INT headerLength
= 0;
535 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hHttpRequest
;
536 LPWININETHTTPSESSIONA lpwhs
= NULL
;
537 LPWININETAPPINFOA hIC
= NULL
;
539 TRACE("0x%08lx\n", (ULONG
)hHttpRequest
);
541 /* Verify our tree of internet handles */
542 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
544 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
548 lpwhs
= (LPWININETHTTPSESSIONA
) lpwhr
->hdr
.lpwhparent
;
549 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
551 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
555 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
556 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
558 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
562 /* Clear any error information */
563 INTERNET_SetLastError(0);
565 /* We must have a verb */
566 if (NULL
== lpwhr
->lpszVerb
)
571 /* If we don't have a path we set it to root */
572 if (NULL
== lpwhr
->lpszPath
)
573 lpwhr
->lpszPath
= HEAP_strdupA(GetProcessHeap(), 0, "/");
575 /* Calculate length of request string */
577 strlen(lpwhr
->lpszVerb
) +
578 strlen(lpwhr
->lpszPath
) +
579 (lpwhr
->lpszHostName
? (strlen(HTTPHOSTHEADER
) + strlen(lpwhr
->lpszHostName
)) : 0) +
583 /* Add length of passed headers */
586 headerLength
= -1 == dwHeaderLength
? strlen(lpszHeaders
) : dwHeaderLength
;
587 requestStringLen
+= headerLength
+ 2; /* \r\n */
590 /* Calculate length of custom request headers */
591 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
593 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
595 requestStringLen
+= strlen(lpwhr
->pCustHeaders
[i
].lpszField
) +
596 strlen(lpwhr
->pCustHeaders
[i
].lpszValue
) + 4; /*: \r\n */
600 /* Calculate the length of standard request headers */
601 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
603 if (lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
)
605 requestStringLen
+= strlen(lpwhr
->StdHeaders
[i
].lpszField
) +
606 strlen(lpwhr
->StdHeaders
[i
].lpszValue
) + 4; /*: \r\n */
610 /* Allocate string to hold entire request */
611 requestString
= HeapAlloc(GetProcessHeap(), 0, requestStringLen
+ 1);
612 if (NULL
== requestString
)
614 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
618 /* Build request string */
619 cnt
= sprintf(requestString
, "%s %s%s%s",
622 lpwhr
->lpszHostName
? (HTTPHEADER HTTPHOSTHEADER
) : HTTPHEADER
,
623 lpwhr
->lpszHostName
? lpwhr
->lpszHostName
: "");
625 /* Append standard request headers */
626 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
628 if (lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
)
630 cnt
+= sprintf(requestString
+ cnt
, "\r\n%s: %s",
631 lpwhr
->StdHeaders
[i
].lpszField
, lpwhr
->StdHeaders
[i
].lpszValue
);
635 /* Append custom request heades */
636 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
638 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
640 cnt
+= sprintf(requestString
+ cnt
, "\r\n%s: %s",
641 lpwhr
->pCustHeaders
[i
].lpszField
, lpwhr
->pCustHeaders
[i
].lpszValue
);
645 /* Append passed request headers */
648 strcpy(requestString
+ cnt
, "\r\n");
650 strcpy(requestString
+ cnt
, lpszHeaders
);
654 /* Set termination string for request */
655 strcpy(requestString
+ cnt
, "\r\n\r\n");
657 if (hIC
->lpfnStatusCB
)
658 hIC
->lpfnStatusCB(hHttpRequest
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
660 TRACE("(%s) len(%d)\n", requestString
, requestStringLen
);
661 /* Send the request and store the results */
662 if (!HTTP_OpenConnection(lpwhr
))
665 cnt
= INTERNET_WriteDataToStream(lpwhr
->nSocketFD
, requestString
, requestStringLen
);
670 if (HTTP_GetResponseHeaders(lpwhr
))
676 HeapFree(GetProcessHeap(), 0, requestString
);
678 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
&& hIC
->lpfnStatusCB
)
680 INTERNET_ASYNC_RESULT iar
;
682 iar
.dwResult
= (DWORD
)bSuccess
;
683 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
684 hIC
->lpfnStatusCB(hHttpRequest
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
685 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
693 /***********************************************************************
694 * HTTP_Connect (internal)
696 * Create http session handle
699 * HINTERNET a session handle on success
703 HINTERNET
HTTP_Connect(HINTERNET hInternet
, LPCSTR lpszServerName
,
704 INTERNET_PORT nServerPort
, LPCSTR lpszUserName
,
705 LPCSTR lpszPassword
, DWORD dwFlags
, DWORD dwContext
)
707 BOOL bSuccess
= FALSE
;
708 LPWININETAPPINFOA hIC
= NULL
;
709 LPWININETHTTPSESSIONA lpwhs
= NULL
;
713 if (((LPWININETHANDLEHEADER
)hInternet
)->htype
!= WH_HINIT
)
716 hIC
= (LPWININETAPPINFOA
) hInternet
;
718 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONA
));
721 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
725 if (hIC
->lpfnStatusCB
)
726 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
727 (LPVOID
)lpszServerName
, strlen(lpszServerName
));
729 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
730 nServerPort
= INTERNET_DEFAULT_HTTP_PORT
;
732 if (!GetAddress(lpszServerName
, nServerPort
, &lpwhs
->phostent
, &lpwhs
->socketAddress
))
734 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
738 if (hIC
->lpfnStatusCB
)
739 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
740 (LPVOID
)lpszServerName
, strlen(lpszServerName
));
742 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
743 lpwhs
->hdr
.lpwhparent
= (LPWININETHANDLEHEADER
)hInternet
;
744 lpwhs
->hdr
.dwFlags
= dwFlags
;
745 lpwhs
->hdr
.dwContext
= dwContext
;
746 if (NULL
!= lpszServerName
)
747 lpwhs
->lpszServerName
= HEAP_strdupA(GetProcessHeap(), 0, lpszServerName
);
748 if (NULL
!= lpszUserName
)
749 lpwhs
->lpszUserName
= HEAP_strdupA(GetProcessHeap(), 0, lpszUserName
);
750 lpwhs
->nServerPort
= nServerPort
;
752 if (hIC
->lpfnStatusCB
)
754 INTERNET_ASYNC_RESULT iar
;
756 iar
.dwResult
= (DWORD
)lpwhs
;
757 iar
.dwError
= ERROR_SUCCESS
;
759 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
760 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
766 if (!bSuccess
&& lpwhs
)
768 HeapFree(GetProcessHeap(), 0, lpwhs
);
772 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
&& hIC
->lpfnStatusCB
)
774 INTERNET_ASYNC_RESULT iar
;
776 iar
.dwResult
= (DWORD
)lpwhs
;
777 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
778 hIC
->lpfnStatusCB(hInternet
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
779 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
782 return (HINTERNET
)lpwhs
;
786 /***********************************************************************
787 * HTTP_OpenConnection (internal)
789 * Connect to a web server
796 BOOL
HTTP_OpenConnection(LPWININETHTTPREQA lpwhr
)
798 BOOL bSuccess
= FALSE
;
800 LPWININETHTTPSESSIONA lpwhs
;
804 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
806 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
810 lpwhs
= (LPWININETHTTPSESSIONA
)lpwhr
->hdr
.lpwhparent
;
812 lpwhr
->nSocketFD
= socket(lpwhs
->phostent
->h_addrtype
,SOCK_STREAM
,0);
813 if (INVALID_SOCKET
== lpwhr
->nSocketFD
)
815 WARN("Socket creation failed\n");
819 result
= connect(lpwhr
->nSocketFD
, (struct sockaddr
*)&lpwhs
->socketAddress
,
820 sizeof(lpwhs
->socketAddress
));
822 if (SOCKET_ERROR
== result
)
824 WARN("Unable to connect to host: %d\n", errno
);
831 TRACE(": %d\n", bSuccess
);
836 /***********************************************************************
837 * HTTP_GetResponseHeaders (internal)
839 * Read server response
846 BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr
)
849 CHAR buffer
[MAX_REPLY_LEN
];
850 DWORD buflen
= MAX_REPLY_LEN
;
851 BOOL bSuccess
= FALSE
;
852 CHAR value
[MAX_FIELD_VALUE_LEN
], field
[MAX_FIELD_LEN
];
856 if (INVALID_SOCKET
== lpwhr
->nSocketFD
)
860 * We should first receive 'HTTP/1.x nnn' where nnn is the status code.
862 if (!INTERNET_GetNextLine(lpwhr
->nSocketFD
, buffer
, &buflen
))
865 if (strncmp(buffer
, "HTTP", 4) != 0)
869 HTTP_ProcessHeader(lpwhr
, "Status", buffer
+9, (HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
));
871 /* Parse each response line */
874 buflen
= MAX_REPLY_LEN
;
875 if (INTERNET_GetNextLine(lpwhr
->nSocketFD
, buffer
, &buflen
))
877 if (!HTTP_InterpretHttpHeader(buffer
, field
, MAX_FIELD_LEN
, value
, MAX_FIELD_VALUE_LEN
))
880 HTTP_ProcessHeader(lpwhr
, field
, value
, (HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
));
898 /***********************************************************************
899 * HTTP_InterpretHttpHeader (internal)
901 * Parse server response
908 INT
stripSpaces(LPCSTR lpszSrc
, LPSTR lpszStart
, INT
*len
)
915 while (*lpszSrc
== ' ' && *lpszSrc
!= '\0')
919 while(*lpsztmp
!= '\0')
922 srclen
= lpsztmp
- lpszSrc
+ 1;
927 *len
= min(*len
, srclen
);
928 strncpy(lpszStart
, lpszSrc
, *len
);
929 lpszStart
[*len
] = '\0';
935 BOOL
HTTP_InterpretHttpHeader(LPSTR buffer
, LPSTR field
, INT fieldlen
, LPSTR value
, INT valuelen
)
938 BOOL bSuccess
= FALSE
;
945 pd
= strchr(buffer
, ':');
949 if (stripSpaces(buffer
, field
, &fieldlen
) > 0)
951 if (stripSpaces(pd
+1, value
, &valuelen
) > 0)
956 TRACE("%d: field(%s) Value(%s)\n", bSuccess
, field
, value
);
961 /***********************************************************************
962 * HTTP_GetStdHeaderIndex (internal)
964 * Lookup field index in standard http header array
966 * FIXME: This should be stuffed into a hash table
968 INT
HTTP_GetStdHeaderIndex(LPCSTR lpszField
)
972 if (!strcasecmp(lpszField
, "Content-Length"))
973 index
= HTTP_QUERY_CONTENT_LENGTH
;
974 else if (!strcasecmp(lpszField
,"Status"))
975 index
= HTTP_QUERY_STATUS_CODE
;
976 else if (!strcasecmp(lpszField
,"Content-Type"))
977 index
= HTTP_QUERY_CONTENT_TYPE
;
978 else if (!strcasecmp(lpszField
,"Last-Modified"))
979 index
= HTTP_QUERY_LAST_MODIFIED
;
980 else if (!strcasecmp(lpszField
,"Location"))
981 index
= HTTP_QUERY_LOCATION
;
982 else if (!strcasecmp(lpszField
,"Accept"))
983 index
= HTTP_QUERY_ACCEPT
;
984 else if (!strcasecmp(lpszField
,"Referer"))
985 index
= HTTP_QUERY_REFERER
;
986 else if (!strcasecmp(lpszField
,"Content-Transfer-Encoding"))
987 index
= HTTP_QUERY_CONTENT_TRANSFER_ENCODING
;
988 else if (!strcasecmp(lpszField
,"Date"))
989 index
= HTTP_QUERY_DATE
;
990 else if (!strcasecmp(lpszField
,"Server"))
991 index
= HTTP_QUERY_SERVER
;
992 else if (!strcasecmp(lpszField
,"Connection"))
993 index
= HTTP_QUERY_CONNECTION
;
994 else if (!strcasecmp(lpszField
,"ETag"))
995 index
= HTTP_QUERY_ETAG
;
996 else if (!strcasecmp(lpszField
,"Accept-Ranges"))
997 index
= HTTP_QUERY_ACCEPT_RANGES
;
998 else if (!strcasecmp(lpszField
,"Expires"))
999 index
= HTTP_QUERY_EXPIRES
;
1000 else if (!strcasecmp(lpszField
,"Mime-Version"))
1001 index
= HTTP_QUERY_MIME_VERSION
;
1004 FIXME("Couldn't find %s in standard header table\n", lpszField
);
1011 /***********************************************************************
1012 * HTTP_ProcessHeader (internal)
1014 * Stuff header into header tables according to <dwModifier>
1018 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1020 BOOL
HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr
, LPCSTR field
, LPCSTR value
, DWORD dwModifier
)
1022 LPHTTPHEADERA lphttpHdr
= NULL
;
1023 BOOL bSuccess
= FALSE
;
1026 TRACE("%s:%s - 0x%08x\n", field
, value
, (unsigned int)dwModifier
);
1028 /* Adjust modifier flags */
1029 if (dwModifier
& COALESCEFLASG
)
1030 dwModifier
|= HTTP_ADDHDR_FLAG_ADD
;
1032 /* Try to get index into standard header array */
1033 index
= HTTP_GetStdHeaderIndex(field
);
1036 lphttpHdr
= &lpwhr
->StdHeaders
[index
];
1038 else /* Find or create new custom header */
1040 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
);
1043 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
1047 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
1053 hdr
.lpszField
= (LPSTR
)field
;
1054 hdr
.lpszValue
= (LPSTR
)value
;
1055 hdr
.wFlags
= hdr
.wCount
= 0;
1057 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
1058 hdr
.wFlags
|= HDR_ISREQUEST
;
1060 index
= HTTP_InsertCustomHeader(lpwhr
, &hdr
);
1065 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
1066 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
1068 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
1070 if (!lphttpHdr
->lpszValue
&& (dwModifier
& (HTTP_ADDHDR_FLAG_ADD
|HTTP_ADDHDR_FLAG_ADD_IF_NEW
)))
1074 if (!lpwhr
->StdHeaders
[index
].lpszField
)
1076 lphttpHdr
->lpszField
= HEAP_strdupA(GetProcessHeap(), 0, field
);
1078 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
1079 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
1082 slen
= strlen(value
) + 1;
1083 lphttpHdr
->lpszValue
= HeapAlloc(GetProcessHeap(), 0, slen
);
1084 if (lphttpHdr
->lpszValue
)
1086 memcpy(lphttpHdr
->lpszValue
, value
, slen
);
1091 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1094 else if (lphttpHdr
->lpszValue
)
1096 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
1101 len
= strlen(value
);
1105 //! if custom header delete from array
1106 HeapFree(GetProcessHeap(), 0, lphttpHdr
->lpszValue
);
1107 lphttpHdr
->lpszValue
= NULL
;
1112 lpsztmp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lphttpHdr
->lpszValue
, len
+1);
1115 lphttpHdr
->lpszValue
= lpsztmp
;
1116 strcpy(lpsztmp
, value
);
1121 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1125 else if (dwModifier
& COALESCEFLASG
)
1130 INT origlen
= strlen(lphttpHdr
->lpszValue
);
1131 INT valuelen
= strlen(value
);
1133 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
1136 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
1138 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
1141 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
1144 len
= origlen
+ valuelen
+ (ch
> 0) ? 1 : 0;
1146 lpsztmp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lphttpHdr
->lpszValue
, len
+1);
1149 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
1152 lphttpHdr
->lpszValue
[origlen
] = ch
;
1156 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
);
1157 lphttpHdr
->lpszValue
[len
] = '\0';
1162 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1171 /***********************************************************************
1172 * HTTP_CloseConnection (internal)
1174 * Close socket connection
1177 VOID
HTTP_CloseConnection(LPWININETHTTPREQA lpwhr
)
1179 if (lpwhr
->nSocketFD
!= INVALID_SOCKET
)
1181 close(lpwhr
->nSocketFD
);
1182 lpwhr
->nSocketFD
= INVALID_SOCKET
;
1187 /***********************************************************************
1188 * HTTP_CloseHTTPRequestHandle (internal)
1190 * Deallocate request handle
1193 void HTTP_CloseHTTPRequestHandle(LPWININETHTTPREQA lpwhr
)
1199 if (lpwhr
->nSocketFD
!= INVALID_SOCKET
)
1200 HTTP_CloseConnection(lpwhr
);
1202 if (lpwhr
->lpszPath
)
1203 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
1204 if (lpwhr
->lpszVerb
)
1205 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
1206 if (lpwhr
->lpszHostName
)
1207 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszHostName
);
1209 for (i
= 0; i
<= HTTP_QUERY_MAX
; i
++)
1211 if (lpwhr
->StdHeaders
[i
].lpszField
)
1212 HeapFree(GetProcessHeap(), 0, lpwhr
->StdHeaders
[i
].lpszField
);
1213 if (lpwhr
->StdHeaders
[i
].lpszValue
)
1214 HeapFree(GetProcessHeap(), 0, lpwhr
->StdHeaders
[i
].lpszValue
);
1217 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
1219 if (lpwhr
->pCustHeaders
[i
].lpszField
)
1220 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszField
);
1221 if (lpwhr
->pCustHeaders
[i
].lpszValue
)
1222 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszValue
);
1225 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
);
1226 HeapFree(GetProcessHeap(), 0, lpwhr
);
1230 /***********************************************************************
1231 * HTTP_CloseHTTPSessionHandle (internal)
1233 * Deallocate session handle
1236 void HTTP_CloseHTTPSessionHandle(LPWININETHTTPSESSIONA lpwhs
)
1240 if (lpwhs
->lpszServerName
)
1241 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
1242 if (lpwhs
->lpszUserName
)
1243 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
1244 HeapFree(GetProcessHeap(), 0, lpwhs
);
1248 /***********************************************************************
1249 * HTTP_GetCustomHeaderIndex (internal)
1251 * Return index of custom header from header array
1254 INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr
, LPCSTR lpszField
)
1258 TRACE("%s\n", lpszField
);
1260 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
1262 if (!strcasecmp(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
1267 if (index
>= lpwhr
->nCustHeaders
)
1270 TRACE("Return: %d\n", index
);
1275 /***********************************************************************
1276 * HTTP_InsertCustomHeader (internal)
1278 * Insert header into array
1281 INT
HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr
, LPHTTPHEADERA lpHdr
)
1284 LPHTTPHEADERA lph
= NULL
;
1286 TRACE("%s: %s\n", lpHdr
->lpszField
, lpHdr
->lpszValue
);
1287 count
= lpwhr
->nCustHeaders
+ 1;
1289 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERA
) * count
);
1291 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERA
) * count
);
1295 lpwhr
->pCustHeaders
= lph
;
1296 lpwhr
->pCustHeaders
[count
-1].lpszField
= HEAP_strdupA(GetProcessHeap(), 0, lpHdr
->lpszField
);
1297 lpwhr
->pCustHeaders
[count
-1].lpszValue
= HEAP_strdupA(GetProcessHeap(), 0, lpHdr
->lpszValue
);
1298 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
1299 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
1300 lpwhr
->nCustHeaders
++;
1304 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1308 TRACE("%d <--\n", count
-1);
1313 /***********************************************************************
1314 * HTTP_DeleteCustomHeader (internal)
1316 * Delete header from array
1319 BOOL
HTTP_DeleteCustomHeader(INT index
)