2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_SOCKET_H
29 # include <sys/socket.h>
45 #define NO_SHLWAPI_STREAM
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
53 #define HTTPHEADER " HTTP/1.0"
54 #define HTTPHOSTHEADER "\r\nHost: "
55 #define MAXHOSTNAME 100
56 #define MAX_FIELD_VALUE_LEN 256
57 #define MAX_FIELD_LEN 256
60 #define HTTP_REFERER "Referer"
61 #define HTTP_ACCEPT "Accept"
63 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
64 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
65 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
66 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
67 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
68 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
69 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
72 BOOL HTTP_OpenConnection(LPWININETHTTPREQA lpwhr);
73 int HTTP_WriteDataToStream(LPWININETHTTPREQA lpwhr,
74 void *Buffer, int BytesToWrite);
75 int HTTP_ReadDataFromStream(LPWININETHTTPREQA lpwhr,
76 void *Buffer, int BytesToRead);
77 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr);
78 BOOL HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr, LPCSTR field, LPCSTR value, DWORD dwModifier);
79 void HTTP_CloseConnection(LPWININETHTTPREQA lpwhr);
80 BOOL HTTP_InterpretHttpHeader(LPSTR buffer, LPSTR field, INT fieldlen, LPSTR value, INT valuelen);
81 INT HTTP_GetStdHeaderIndex(LPCSTR lpszField);
82 INT HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr, LPHTTPHEADERA lpHdr);
83 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr, LPCSTR lpszField);
85 inline static LPSTR HTTP_strdup( LPCSTR str )
87 LPSTR ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
88 if (ret) strcpy( ret, str );
92 /***********************************************************************
93 * HttpAddRequestHeadersA (WININET.@)
95 * Adds one or more HTTP header to the request handler
102 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
103 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
108 CHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
109 BOOL bSuccess = FALSE;
110 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
114 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
116 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
120 buffer = HTTP_strdup(lpszHeader);
127 while (*lpszEnd != '\0')
129 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
134 if (*lpszEnd == '\0')
139 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
140 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
142 lpszStart = lpszEnd + 2; /* Jump over \0\n */
146 HeapFree(GetProcessHeap(), 0, buffer);
151 /***********************************************************************
152 * HttpOpenRequestA (WININET.@)
154 * Open a HTTP request handle
157 * HINTERNET a HTTP request handle on success
161 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
162 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
163 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
164 DWORD dwFlags, DWORD dwContext)
166 LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) hHttpSession;
167 LPWININETAPPINFOA hIC = NULL;
171 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
173 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
176 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
179 * My tests seem to show that the windows version does not
180 * become asynchronous until after this point. And anyhow
181 * if this call was asynchronous then how would you get the
182 * necessary HINTERNET pointer returned by this function.
184 * I am leaving this here just in case I am wrong
186 * if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
190 WORKREQUEST workRequest;
192 workRequest.asyncall = HTTPOPENREQUESTA;
193 workRequest.HFTPSESSION = (DWORD)hHttpSession;
194 workRequest.LPSZVERB = (DWORD)HTTP_strdup(lpszVerb);
195 workRequest.LPSZOBJECTNAME = (DWORD)HTTP_strdup(lpszObjectName);
197 workRequest.LPSZVERSION = (DWORD)HTTP_strdup(lpszVersion);
199 workRequest.LPSZVERSION = 0;
201 workRequest.LPSZREFERRER = (DWORD)HTTP_strdup(lpszReferrer);
203 workRequest.LPSZREFERRER = 0;
204 workRequest.LPSZACCEPTTYPES = (DWORD)lpszAcceptTypes;
205 workRequest.DWFLAGS = dwFlags;
206 workRequest.DWCONTEXT = dwContext;
208 INTERNET_AsyncCall(&workRequest);
213 return HTTP_HttpOpenRequestA(hHttpSession, lpszVerb, lpszObjectName,
214 lpszVersion, lpszReferrer, lpszAcceptTypes, dwFlags, dwContext);
219 /***********************************************************************
220 * HTTP_HttpOpenRequestA (internal)
222 * Open a HTTP request handle
225 * HINTERNET a HTTP request handle on success
229 HINTERNET WINAPI HTTP_HttpOpenRequestA(HINTERNET hHttpSession,
230 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
231 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
232 DWORD dwFlags, DWORD dwContext)
234 LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) hHttpSession;
235 LPWININETAPPINFOA hIC = NULL;
236 LPWININETHTTPREQA lpwhr;
240 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
242 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
246 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
248 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQA));
251 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
252 return (HINTERNET) NULL;
255 lpwhr->hdr.htype = WH_HHTTPREQ;
256 lpwhr->hdr.lpwhparent = hHttpSession;
257 lpwhr->hdr.dwFlags = dwFlags;
258 lpwhr->hdr.dwContext = dwContext;
259 lpwhr->nSocketFD = -1;
261 if (NULL != lpszObjectName && strlen(lpszObjectName)) {
264 rc = UrlEscapeA(lpszObjectName, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
266 needed = strlen(lpszObjectName)+1;
267 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed);
268 rc = UrlEscapeA(lpszObjectName, lpwhr->lpszPath, &needed,
269 URL_ESCAPE_SPACES_ONLY);
272 ERR("Unable to escape string!(%s) (%ld)\n",lpszObjectName,rc);
273 strcpy(lpwhr->lpszPath,lpszObjectName);
277 if (NULL != lpszReferrer && strlen(lpszReferrer))
278 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
281 if (NULL != lpszAcceptTypes && strlen(*lpszAcceptTypes))
282 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, *lpszAcceptTypes, HTTP_ADDHDR_FLAG_COALESCE);
284 if (NULL == lpszVerb)
285 lpwhr->lpszVerb = HTTP_strdup("GET");
286 else if (strlen(lpszVerb))
287 lpwhr->lpszVerb = HTTP_strdup(lpszVerb);
289 if (NULL != lpszReferrer)
291 char buf[MAXHOSTNAME];
292 URL_COMPONENTSA UrlComponents;
294 UrlComponents.lpszExtraInfo = NULL;
295 UrlComponents.lpszPassword = NULL;
296 UrlComponents.lpszScheme = NULL;
297 UrlComponents.lpszUrlPath = NULL;
298 UrlComponents.lpszUserName = NULL;
299 UrlComponents.lpszHostName = buf;
300 UrlComponents.dwHostNameLength = MAXHOSTNAME;
302 InternetCrackUrlA(lpszReferrer, 0, 0, &UrlComponents);
303 if (strlen(UrlComponents.lpszHostName))
304 lpwhr->lpszHostName = HTTP_strdup(UrlComponents.lpszHostName);
306 lpwhr->lpszHostName = HTTP_strdup(lpwhs->lpszServerName);
309 if (hIC->lpfnStatusCB)
311 INTERNET_ASYNC_RESULT iar;
313 iar.dwResult = (DWORD)lpwhr;
314 iar.dwError = ERROR_SUCCESS;
316 SendAsyncCallback(hIC, hHttpSession, dwContext,
317 INTERNET_STATUS_HANDLE_CREATED, &iar,
318 sizeof(INTERNET_ASYNC_RESULT));
322 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
326 * According to my tests. The name is not resolved until a request is Opened
328 SendAsyncCallback(hIC, hHttpSession, dwContext,
329 INTERNET_STATUS_RESOLVING_NAME,
330 lpwhs->lpszServerName,
331 strlen(lpwhs->lpszServerName)+1);
333 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
334 &lpwhs->phostent, &lpwhs->socketAddress))
336 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
340 SendAsyncCallback(hIC, hHttpSession, lpwhr->hdr.dwContext,
341 INTERNET_STATUS_NAME_RESOLVED,
342 &(lpwhs->socketAddress),
343 sizeof(struct sockaddr_in));
346 return (HINTERNET) lpwhr;
350 /***********************************************************************
351 * HttpQueryInfoA (WININET.@)
353 * Queries for information about an HTTP request
360 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
361 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
363 LPHTTPHEADERA lphttpHdr = NULL;
364 BOOL bSuccess = FALSE;
365 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
367 TRACE("(0x%08lx)--> %ld\n", dwInfoLevel, dwInfoLevel);
369 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
371 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
375 /* Find requested header structure */
376 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
378 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPSTR)lpBuffer);
383 lphttpHdr = &lpwhr->pCustHeaders[index];
387 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
389 if (index == HTTP_QUERY_RAW_HEADERS_CRLF || index == HTTP_QUERY_RAW_HEADERS)
391 INT i, delim, size = 0, cnt = 0;
393 delim = index == HTTP_QUERY_RAW_HEADERS_CRLF ? 2 : 1;
395 /* Calculate length of custom reuqest headers */
396 for (i = 0; i < lpwhr->nCustHeaders; i++)
398 if ((~lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST) && lpwhr->pCustHeaders[i].lpszField &&
399 lpwhr->pCustHeaders[i].lpszValue)
401 size += strlen(lpwhr->pCustHeaders[i].lpszField) +
402 strlen(lpwhr->pCustHeaders[i].lpszValue) + delim + 2;
406 /* Calculate the length of stadard request headers */
407 for (i = 0; i <= HTTP_QUERY_MAX; i++)
409 if ((~lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST) && lpwhr->StdHeaders[i].lpszField &&
410 lpwhr->StdHeaders[i].lpszValue)
412 size += strlen(lpwhr->StdHeaders[i].lpszField) +
413 strlen(lpwhr->StdHeaders[i].lpszValue) + delim + 2;
418 if (size + 1 > *lpdwBufferLength)
420 *lpdwBufferLength = size + 1;
421 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
425 /* Append standard request heades */
426 for (i = 0; i <= HTTP_QUERY_MAX; i++)
428 if ((~lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST) &&
429 lpwhr->StdHeaders[i].lpszField &&
430 lpwhr->StdHeaders[i].lpszValue)
432 cnt += sprintf((char*)lpBuffer + cnt, "%s: %s%s", lpwhr->StdHeaders[i].lpszField, lpwhr->StdHeaders[i].lpszValue,
433 index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "\0");
437 /* Append custom request heades */
438 for (i = 0; i < lpwhr->nCustHeaders; i++)
440 if ((~lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST) &&
441 lpwhr->pCustHeaders[i].lpszField &&
442 lpwhr->pCustHeaders[i].lpszValue)
444 cnt += sprintf((char*)lpBuffer + cnt, "%s: %s%s",
445 lpwhr->pCustHeaders[i].lpszField, lpwhr->pCustHeaders[i].lpszValue,
446 index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "\0");
450 strcpy((char*)lpBuffer + cnt, index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "");
452 *lpdwBufferLength = cnt + delim;
456 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
458 lphttpHdr = &lpwhr->StdHeaders[index];
464 /* Ensure header satisifies requested attributes */
465 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
466 (~lphttpHdr->wFlags & HDR_ISREQUEST))
469 /* coalesce value to reuqested type */
470 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
472 *(int *)lpBuffer = atoi(lphttpHdr->lpszValue);
475 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
481 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
483 tmpTM = *gmtime(&tmpTime);
484 STHook = (SYSTEMTIME *) lpBuffer;
488 STHook->wDay = tmpTM.tm_mday;
489 STHook->wHour = tmpTM.tm_hour;
490 STHook->wMilliseconds = 0;
491 STHook->wMinute = tmpTM.tm_min;
492 STHook->wDayOfWeek = tmpTM.tm_wday;
493 STHook->wMonth = tmpTM.tm_mon + 1;
494 STHook->wSecond = tmpTM.tm_sec;
495 STHook->wYear = tmpTM.tm_year;
499 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
501 if (*lpdwIndex >= lphttpHdr->wCount)
503 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
507 /* Copy strncpy(lpBuffer, lphttpHdr[*lpdwIndex], len); */
513 INT len = strlen(lphttpHdr->lpszValue);
515 if (len + 1 > *lpdwBufferLength)
517 *lpdwBufferLength = len + 1;
518 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
522 strncpy(lpBuffer, lphttpHdr->lpszValue, len);
523 ((char*)lpBuffer)[len]=0;
524 *lpdwBufferLength = len;
529 TRACE("%d <--\n", bSuccess);
534 /***********************************************************************
535 * HttpSendRequestExA (WININET.@)
537 * Sends the specified request to the HTTP server and allows chunked
540 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
541 LPINTERNET_BUFFERSA lpBuffersIn,
542 LPINTERNET_BUFFERSA lpBuffersOut,
543 DWORD dwFlags, DWORD dwContext)
545 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
546 lpBuffersOut, dwFlags, dwContext);
550 /***********************************************************************
551 * HttpSendRequestA (WININET.@)
553 * Sends the specified request to the HTTP server
560 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
561 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
563 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
564 LPWININETHTTPSESSIONA lpwhs = NULL;
565 LPWININETAPPINFOA hIC = NULL;
567 TRACE("0x%08lx\n", (unsigned long)hHttpRequest);
569 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
571 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
575 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
576 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
578 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
582 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
583 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
585 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
589 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
591 WORKREQUEST workRequest;
593 workRequest.asyncall = HTTPSENDREQUESTA;
594 workRequest.HFTPSESSION = (DWORD)hHttpRequest;
596 workRequest.LPSZHEADER = (DWORD)HTTP_strdup(lpszHeaders);
598 workRequest.LPSZHEADER = 0;
599 workRequest.DWHEADERLENGTH = dwHeaderLength;
600 workRequest.LPOPTIONAL = (DWORD)lpOptional;
601 workRequest.DWOPTIONALLENGTH = dwOptionalLength;
603 INTERNET_AsyncCall(&workRequest);
605 * This is from windows. I do not know what the name is
612 return HTTP_HttpSendRequestA(hHttpRequest, lpszHeaders,
613 dwHeaderLength, lpOptional, dwOptionalLength);
618 /***********************************************************************
619 * HTTP_HttpSendRequestA (internal)
621 * Sends the specified request to the HTTP server
628 BOOL WINAPI HTTP_HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
629 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
633 BOOL bSuccess = FALSE;
634 LPSTR requestString = NULL;
635 INT requestStringLen;
637 INT headerLength = 0;
638 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
639 LPWININETHTTPSESSIONA lpwhs = NULL;
640 LPWININETAPPINFOA hIC = NULL;
642 TRACE("--> 0x%08lx\n", (ULONG)hHttpRequest);
644 /* Verify our tree of internet handles */
645 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
647 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
651 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
652 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
654 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
658 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
659 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
661 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
665 /* Clear any error information */
666 INTERNET_SetLastError(0);
669 /* We must have a verb */
670 if (NULL == lpwhr->lpszVerb)
675 /* If we don't have a path we set it to root */
676 if (NULL == lpwhr->lpszPath)
677 lpwhr->lpszPath = HTTP_strdup("/");
679 if(lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
681 char *fixurl = HeapAlloc(GetProcessHeap(), 0, strlen(lpwhr->lpszPath) + 2);
683 strcpy(fixurl + 1, lpwhr->lpszPath);
684 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
685 lpwhr->lpszPath = fixurl;
688 /* Calculate length of request string */
690 strlen(lpwhr->lpszVerb) +
691 strlen(lpwhr->lpszPath) +
692 (lpwhr->lpszHostName ? (strlen(HTTPHOSTHEADER) + strlen(lpwhr->lpszHostName)) : 0) +
696 /* Add length of passed headers */
699 headerLength = -1 == dwHeaderLength ? strlen(lpszHeaders) : dwHeaderLength;
700 requestStringLen += headerLength + 2; /* \r\n */
703 /* Calculate length of custom request headers */
704 for (i = 0; i < lpwhr->nCustHeaders; i++)
706 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
708 requestStringLen += strlen(lpwhr->pCustHeaders[i].lpszField) +
709 strlen(lpwhr->pCustHeaders[i].lpszValue) + 4; /*: \r\n */
713 /* Calculate the length of standard request headers */
714 for (i = 0; i <= HTTP_QUERY_MAX; i++)
716 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
718 requestStringLen += strlen(lpwhr->StdHeaders[i].lpszField) +
719 strlen(lpwhr->StdHeaders[i].lpszValue) + 4; /*: \r\n */
723 /* Allocate string to hold entire request */
724 requestString = HeapAlloc(GetProcessHeap(), 0, requestStringLen + 1);
725 if (NULL == requestString)
727 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
731 /* Build request string */
732 cnt = sprintf(requestString, "%s %s%s%s",
735 lpwhr->lpszHostName ? (HTTPHEADER HTTPHOSTHEADER) : HTTPHEADER,
736 lpwhr->lpszHostName ? lpwhr->lpszHostName : "");
738 /* Append standard request headers */
739 for (i = 0; i <= HTTP_QUERY_MAX; i++)
741 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
743 cnt += sprintf(requestString + cnt, "\r\n%s: %s",
744 lpwhr->StdHeaders[i].lpszField, lpwhr->StdHeaders[i].lpszValue);
748 /* Append custom request heades */
749 for (i = 0; i < lpwhr->nCustHeaders; i++)
751 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
753 cnt += sprintf(requestString + cnt, "\r\n%s: %s",
754 lpwhr->pCustHeaders[i].lpszField, lpwhr->pCustHeaders[i].lpszValue);
758 /* Append passed request headers */
761 strcpy(requestString + cnt, "\r\n");
763 strcpy(requestString + cnt, lpszHeaders);
767 /* Set termination string for request */
768 strcpy(requestString + cnt, "\r\n\r\n");
770 TRACE("(%s) len(%d)\n", requestString, requestStringLen);
771 /* Send the request and store the results */
772 if (!HTTP_OpenConnection(lpwhr))
775 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
776 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
778 cnt = send(lpwhr->nSocketFD, requestString, requestStringLen, 0);
780 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
781 INTERNET_STATUS_REQUEST_SENT,
782 &requestStringLen,sizeof(DWORD));
784 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
785 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
790 responseLen = HTTP_GetResponseHeaders(lpwhr);
794 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
795 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
801 HeapFree(GetProcessHeap(), 0, requestString);
804 if (hIC->lpfnStatusCB)
806 INTERNET_ASYNC_RESULT iar;
808 iar.dwResult = (DWORD)bSuccess;
809 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
811 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
812 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
813 sizeof(INTERNET_ASYNC_RESULT));
821 /***********************************************************************
822 * HTTP_Connect (internal)
824 * Create http session handle
827 * HINTERNET a session handle on success
831 HINTERNET HTTP_Connect(HINTERNET hInternet, LPCSTR lpszServerName,
832 INTERNET_PORT nServerPort, LPCSTR lpszUserName,
833 LPCSTR lpszPassword, DWORD dwFlags, DWORD dwContext)
835 BOOL bSuccess = FALSE;
836 LPWININETAPPINFOA hIC = NULL;
837 LPWININETHTTPSESSIONA lpwhs = NULL;
841 if (((LPWININETHANDLEHEADER)hInternet)->htype != WH_HINIT)
844 hIC = (LPWININETAPPINFOA) hInternet;
845 hIC->hdr.dwContext = dwContext;
847 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONA));
850 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
855 * According to my tests. The name is not resolved until a request is sent
858 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
859 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
861 lpwhs->hdr.htype = WH_HHTTPSESSION;
862 lpwhs->hdr.lpwhparent = (LPWININETHANDLEHEADER)hInternet;
863 lpwhs->hdr.dwFlags = dwFlags;
864 lpwhs->hdr.dwContext = dwContext;
865 if (NULL != lpszServerName)
866 lpwhs->lpszServerName = HTTP_strdup(lpszServerName);
867 if (NULL != lpszUserName)
868 lpwhs->lpszUserName = HTTP_strdup(lpszUserName);
869 lpwhs->nServerPort = nServerPort;
871 if (hIC->lpfnStatusCB)
873 INTERNET_ASYNC_RESULT iar;
875 iar.dwResult = (DWORD)lpwhs;
876 iar.dwError = ERROR_SUCCESS;
878 SendAsyncCallback(hIC, hInternet, dwContext,
879 INTERNET_STATUS_HANDLE_CREATED, &iar,
880 sizeof(INTERNET_ASYNC_RESULT));
886 if (!bSuccess && lpwhs)
888 HeapFree(GetProcessHeap(), 0, lpwhs);
893 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
898 return (HINTERNET)lpwhs;
902 /***********************************************************************
903 * HTTP_OpenConnection (internal)
905 * Connect to a web server
912 BOOL HTTP_OpenConnection(LPWININETHTTPREQA lpwhr)
914 BOOL bSuccess = FALSE;
916 LPWININETHTTPSESSIONA lpwhs;
917 LPWININETAPPINFOA hIC = NULL;
922 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
924 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
928 lpwhs = (LPWININETHTTPSESSIONA)lpwhr->hdr.lpwhparent;
930 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
931 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
932 INTERNET_STATUS_CONNECTING_TO_SERVER,
933 &(lpwhs->socketAddress),
934 sizeof(struct sockaddr_in));
936 lpwhr->nSocketFD = socket(lpwhs->phostent->h_addrtype,SOCK_STREAM,0);
937 if (lpwhr->nSocketFD == -1)
939 WARN("Socket creation failed\n");
943 result = connect(lpwhr->nSocketFD, (struct sockaddr *)&lpwhs->socketAddress,
944 sizeof(lpwhs->socketAddress));
948 WARN("Unable to connect to host (%s)\n", strerror(errno));
952 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
953 INTERNET_STATUS_CONNECTED_TO_SERVER,
954 &(lpwhs->socketAddress),
955 sizeof(struct sockaddr_in));
960 TRACE("%d <--\n", bSuccess);
965 /***********************************************************************
966 * HTTP_GetResponseHeaders (internal)
968 * Read server response
975 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr)
978 CHAR buffer[MAX_REPLY_LEN];
979 DWORD buflen = MAX_REPLY_LEN;
980 BOOL bSuccess = FALSE;
982 CHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
986 if (lpwhr->nSocketFD == -1)
990 * HACK peek at the buffer
992 rc = recv(lpwhr->nSocketFD,buffer,buflen,MSG_PEEK);
995 * We should first receive 'HTTP/1.x nnn' where nnn is the status code.
997 buflen = MAX_REPLY_LEN;
998 if (!INTERNET_GetNextLine(lpwhr->nSocketFD, buffer, &buflen))
1001 if (strncmp(buffer, "HTTP", 4) != 0)
1005 HTTP_ProcessHeader(lpwhr, "Status", buffer+9, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
1007 /* Parse each response line */
1010 buflen = MAX_REPLY_LEN;
1011 if (INTERNET_GetNextLine(lpwhr->nSocketFD, buffer, &buflen))
1013 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
1016 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
1038 /***********************************************************************
1039 * HTTP_InterpretHttpHeader (internal)
1041 * Parse server response
1048 INT stripSpaces(LPCSTR lpszSrc, LPSTR lpszStart, INT *len)
1055 while (*lpszSrc == ' ' && *lpszSrc != '\0')
1059 while(*lpsztmp != '\0')
1061 if (*lpsztmp != ' ')
1062 srclen = lpsztmp - lpszSrc + 1;
1067 *len = min(*len, srclen);
1068 strncpy(lpszStart, lpszSrc, *len);
1069 lpszStart[*len] = '\0';
1075 BOOL HTTP_InterpretHttpHeader(LPSTR buffer, LPSTR field, INT fieldlen, LPSTR value, INT valuelen)
1078 BOOL bSuccess = FALSE;
1085 pd = strchr(buffer, ':');
1089 if (stripSpaces(buffer, field, &fieldlen) > 0)
1091 if (stripSpaces(pd+1, value, &valuelen) > 0)
1096 TRACE("%d: field(%s) Value(%s)\n", bSuccess, field, value);
1101 /***********************************************************************
1102 * HTTP_GetStdHeaderIndex (internal)
1104 * Lookup field index in standard http header array
1106 * FIXME: This should be stuffed into a hash table
1108 INT HTTP_GetStdHeaderIndex(LPCSTR lpszField)
1112 if (!strcasecmp(lpszField, "Content-Length"))
1113 index = HTTP_QUERY_CONTENT_LENGTH;
1114 else if (!strcasecmp(lpszField,"Status"))
1115 index = HTTP_QUERY_STATUS_CODE;
1116 else if (!strcasecmp(lpszField,"Content-Type"))
1117 index = HTTP_QUERY_CONTENT_TYPE;
1118 else if (!strcasecmp(lpszField,"Last-Modified"))
1119 index = HTTP_QUERY_LAST_MODIFIED;
1120 else if (!strcasecmp(lpszField,"Location"))
1121 index = HTTP_QUERY_LOCATION;
1122 else if (!strcasecmp(lpszField,"Accept"))
1123 index = HTTP_QUERY_ACCEPT;
1124 else if (!strcasecmp(lpszField,"Referer"))
1125 index = HTTP_QUERY_REFERER;
1126 else if (!strcasecmp(lpszField,"Content-Transfer-Encoding"))
1127 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
1128 else if (!strcasecmp(lpszField,"Date"))
1129 index = HTTP_QUERY_DATE;
1130 else if (!strcasecmp(lpszField,"Server"))
1131 index = HTTP_QUERY_SERVER;
1132 else if (!strcasecmp(lpszField,"Connection"))
1133 index = HTTP_QUERY_CONNECTION;
1134 else if (!strcasecmp(lpszField,"ETag"))
1135 index = HTTP_QUERY_ETAG;
1136 else if (!strcasecmp(lpszField,"Accept-Ranges"))
1137 index = HTTP_QUERY_ACCEPT_RANGES;
1138 else if (!strcasecmp(lpszField,"Expires"))
1139 index = HTTP_QUERY_EXPIRES;
1140 else if (!strcasecmp(lpszField,"Mime-Version"))
1141 index = HTTP_QUERY_MIME_VERSION;
1142 else if (!strcasecmp(lpszField,"Pragma"))
1143 index = HTTP_QUERY_PRAGMA;
1144 else if (!strcasecmp(lpszField,"Cache-Control"))
1145 index = HTTP_QUERY_CACHE_CONTROL;
1146 else if (!strcasecmp(lpszField,"Content-Length"))
1147 index = HTTP_QUERY_CONTENT_LENGTH;
1148 else if (!strcasecmp(lpszField,"User-Agent"))
1149 index = HTTP_QUERY_USER_AGENT;
1152 TRACE("Couldn't find %s in standard header table\n", lpszField);
1159 /***********************************************************************
1160 * HTTP_ProcessHeader (internal)
1162 * Stuff header into header tables according to <dwModifier>
1166 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1168 BOOL HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr, LPCSTR field, LPCSTR value, DWORD dwModifier)
1170 LPHTTPHEADERA lphttpHdr = NULL;
1171 BOOL bSuccess = FALSE;
1174 TRACE("--> %s:%s - 0x%08x\n", field, value, (unsigned int)dwModifier);
1176 /* Adjust modifier flags */
1177 if (dwModifier & COALESCEFLASG)
1178 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
1180 /* Try to get index into standard header array */
1181 index = HTTP_GetStdHeaderIndex(field);
1184 lphttpHdr = &lpwhr->StdHeaders[index];
1186 else /* Find or create new custom header */
1188 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
1191 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
1195 lphttpHdr = &lpwhr->pCustHeaders[index];
1201 hdr.lpszField = (LPSTR)field;
1202 hdr.lpszValue = (LPSTR)value;
1203 hdr.wFlags = hdr.wCount = 0;
1205 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1206 hdr.wFlags |= HDR_ISREQUEST;
1208 index = HTTP_InsertCustomHeader(lpwhr, &hdr);
1213 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1214 lphttpHdr->wFlags |= HDR_ISREQUEST;
1216 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
1218 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
1222 if (!lpwhr->StdHeaders[index].lpszField)
1224 lphttpHdr->lpszField = HTTP_strdup(field);
1226 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1227 lphttpHdr->wFlags |= HDR_ISREQUEST;
1230 slen = strlen(value) + 1;
1231 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen);
1232 if (lphttpHdr->lpszValue)
1234 memcpy(lphttpHdr->lpszValue, value, slen);
1239 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1242 else if (lphttpHdr->lpszValue)
1244 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
1249 len = strlen(value);
1253 /* if custom header delete from array */
1254 HeapFree(GetProcessHeap(), 0, lphttpHdr->lpszValue);
1255 lphttpHdr->lpszValue = NULL;
1260 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, len+1);
1263 lphttpHdr->lpszValue = lpsztmp;
1264 strcpy(lpsztmp, value);
1269 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1273 else if (dwModifier & COALESCEFLASG)
1278 INT origlen = strlen(lphttpHdr->lpszValue);
1279 INT valuelen = strlen(value);
1281 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
1284 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
1286 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1289 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
1292 len = origlen + valuelen + (ch > 0) ? 1 : 0;
1294 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, len+1);
1297 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
1300 lphttpHdr->lpszValue[origlen] = ch;
1304 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen);
1305 lphttpHdr->lpszValue[len] = '\0';
1310 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1319 /***********************************************************************
1320 * HTTP_CloseConnection (internal)
1322 * Close socket connection
1325 VOID HTTP_CloseConnection(LPWININETHTTPREQA lpwhr)
1329 LPWININETHTTPSESSIONA lpwhs = NULL;
1330 LPWININETAPPINFOA hIC = NULL;
1332 TRACE("%p\n",lpwhr);
1334 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
1335 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
1337 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1338 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1340 if (lpwhr->nSocketFD != -1)
1342 close(lpwhr->nSocketFD);
1343 lpwhr->nSocketFD = -1;
1346 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1347 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1351 /***********************************************************************
1352 * HTTP_CloseHTTPRequestHandle (internal)
1354 * Deallocate request handle
1357 void HTTP_CloseHTTPRequestHandle(LPWININETHTTPREQA lpwhr)
1360 LPWININETHTTPSESSIONA lpwhs = NULL;
1361 LPWININETAPPINFOA hIC = NULL;
1365 if (lpwhr->nSocketFD != -1)
1366 HTTP_CloseConnection(lpwhr);
1368 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
1369 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
1371 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1372 INTERNET_STATUS_HANDLE_CLOSING, lpwhr,
1375 if (lpwhr->lpszPath)
1376 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1377 if (lpwhr->lpszVerb)
1378 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1379 if (lpwhr->lpszHostName)
1380 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1382 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1384 if (lpwhr->StdHeaders[i].lpszField)
1385 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
1386 if (lpwhr->StdHeaders[i].lpszValue)
1387 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
1390 for (i = 0; i < lpwhr->nCustHeaders; i++)
1392 if (lpwhr->pCustHeaders[i].lpszField)
1393 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1394 if (lpwhr->pCustHeaders[i].lpszValue)
1395 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1398 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1399 HeapFree(GetProcessHeap(), 0, lpwhr);
1403 /***********************************************************************
1404 * HTTP_CloseHTTPSessionHandle (internal)
1406 * Deallocate session handle
1409 void HTTP_CloseHTTPSessionHandle(LPWININETHTTPSESSIONA lpwhs)
1411 LPWININETAPPINFOA hIC = NULL;
1414 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
1416 SendAsyncCallback(hIC, lpwhs, lpwhs->hdr.dwContext,
1417 INTERNET_STATUS_HANDLE_CLOSING, lpwhs,
1420 if (lpwhs->lpszServerName)
1421 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1422 if (lpwhs->lpszUserName)
1423 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1424 HeapFree(GetProcessHeap(), 0, lpwhs);
1428 /***********************************************************************
1429 * HTTP_GetCustomHeaderIndex (internal)
1431 * Return index of custom header from header array
1434 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr, LPCSTR lpszField)
1438 TRACE("%s\n", lpszField);
1440 for (index = 0; index < lpwhr->nCustHeaders; index++)
1442 if (!strcasecmp(lpwhr->pCustHeaders[index].lpszField, lpszField))
1447 if (index >= lpwhr->nCustHeaders)
1450 TRACE("Return: %d\n", index);
1455 /***********************************************************************
1456 * HTTP_InsertCustomHeader (internal)
1458 * Insert header into array
1461 INT HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr, LPHTTPHEADERA lpHdr)
1464 LPHTTPHEADERA lph = NULL;
1466 TRACE("--> %s: %s\n", lpHdr->lpszField, lpHdr->lpszValue);
1467 count = lpwhr->nCustHeaders + 1;
1469 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERA) * count);
1471 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERA) * count);
1475 lpwhr->pCustHeaders = lph;
1476 lpwhr->pCustHeaders[count-1].lpszField = HTTP_strdup(lpHdr->lpszField);
1477 lpwhr->pCustHeaders[count-1].lpszValue = HTTP_strdup(lpHdr->lpszValue);
1478 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
1479 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
1480 lpwhr->nCustHeaders++;
1484 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1488 TRACE("%d <--\n", count-1);
1493 /***********************************************************************
1494 * HTTP_DeleteCustomHeader (internal)
1496 * Delete header from array
1499 BOOL HTTP_DeleteCustomHeader(INT index)