Add a '\r\n' to lpszHeaders if it is not already terminated by
[wine/testsucceed.git] / dlls / wininet / http.c
blob20f509937599217ae3648ab1e0b5eac88cce8972
1 /*
2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
8 * Ulrich Czekalla
9 * Aric Stewart
10 * David Hammerton
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "config.h"
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <errno.h>
40 #include <string.h>
41 #include <time.h>
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wininet.h"
46 #include "winreg.h"
47 #include "winerror.h"
48 #define NO_SHLWAPI_STREAM
49 #include "shlwapi.h"
51 #include "internet.h"
52 #include "wine/debug.h"
53 #include "wine/unicode.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
57 #define HTTPHEADER " HTTP/1.0"
58 #define HTTPHOSTHEADER "\r\nHost: "
59 #define MAXHOSTNAME 100
60 #define MAX_FIELD_VALUE_LEN 256
61 #define MAX_FIELD_LEN 256
64 #define HTTP_REFERER "Referer"
65 #define HTTP_ACCEPT "Accept"
66 #define HTTP_USERAGENT "User-Agent"
68 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
69 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
70 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
71 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
72 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
73 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
74 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
77 BOOL HTTP_OpenConnection(LPWININETHTTPREQA lpwhr);
78 int HTTP_WriteDataToStream(LPWININETHTTPREQA lpwhr,
79 void *Buffer, int BytesToWrite);
80 int HTTP_ReadDataFromStream(LPWININETHTTPREQA lpwhr,
81 void *Buffer, int BytesToRead);
82 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr);
83 BOOL HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr, LPCSTR field, LPCSTR value, DWORD dwModifier);
84 void HTTP_CloseConnection(LPWININETHTTPREQA lpwhr);
85 BOOL HTTP_InterpretHttpHeader(LPSTR buffer, LPSTR field, INT fieldlen, LPSTR value, INT valuelen);
86 INT HTTP_GetStdHeaderIndex(LPCSTR lpszField);
87 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr, LPHTTPHEADERA lpHdr);
88 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr, LPCSTR lpszField);
89 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQA lpwhr, INT index);
91 inline static LPSTR HTTP_strdup( LPCSTR str )
93 LPSTR ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
94 if (ret) strcpy( ret, str );
95 return ret;
98 /***********************************************************************
99 * HttpAddRequestHeadersA (WININET.@)
101 * Adds one or more HTTP header to the request handler
103 * RETURNS
104 * TRUE on success
105 * FALSE on failure
108 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
109 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
111 LPSTR lpszStart;
112 LPSTR lpszEnd;
113 LPSTR buffer;
114 CHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
115 BOOL bSuccess = FALSE;
116 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
118 TRACE("%p, %s, %li, %li\n", hHttpRequest, lpszHeader, dwHeaderLength,
119 dwModifier);
122 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
124 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
125 return FALSE;
128 if (!lpszHeader)
129 return TRUE;
131 TRACE("copying header: %s\n", lpszHeader);
132 buffer = HTTP_strdup(lpszHeader);
133 lpszStart = buffer;
137 lpszEnd = lpszStart;
139 while (*lpszEnd != '\0')
141 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
142 break;
143 lpszEnd++;
146 if (*lpszEnd == '\0')
147 break;
149 *lpszEnd = '\0';
151 TRACE("interpreting header %s\n", debugstr_a(lpszStart));
152 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
153 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
155 lpszStart = lpszEnd + 2; /* Jump over \0\n */
157 } while (bSuccess);
159 HeapFree(GetProcessHeap(), 0, buffer);
160 return bSuccess;
163 /***********************************************************************
164 * HttpEndRequestA (WININET.@)
166 * Ends an HTTP request that was started by HttpSendRequestEx
168 * RETURNS
169 * TRUE if successful
170 * FALSE on failure
173 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
174 DWORD dwFlags, DWORD dwContext)
176 FIXME("stub\n");
177 return FALSE;
180 /***********************************************************************
181 * HttpEndRequestW (WININET.@)
183 * Ends an HTTP request that was started by HttpSendRequestEx
185 * RETURNS
186 * TRUE if successful
187 * FALSE on failure
190 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
191 DWORD dwFlags, DWORD dwContext)
193 FIXME("stub\n");
194 return FALSE;
197 /***********************************************************************
198 * HttpOpenRequestA (WININET.@)
200 * Open a HTTP request handle
202 * RETURNS
203 * HINTERNET a HTTP request handle on success
204 * NULL on failure
207 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
208 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
209 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
210 DWORD dwFlags, DWORD dwContext)
212 LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) hHttpSession;
213 LPWININETAPPINFOA hIC = NULL;
215 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
216 debugstr_a(lpszVerb), lpszObjectName,
217 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
218 dwFlags, dwContext);
219 if(lpszAcceptTypes!=NULL)
221 int i;
222 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
223 TRACE("\taccept type: %s\n",lpszAcceptTypes[i]);
226 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
228 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
229 return FALSE;
231 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
234 * My tests seem to show that the windows version does not
235 * become asynchronous until after this point. And anyhow
236 * if this call was asynchronous then how would you get the
237 * necessary HINTERNET pointer returned by this function.
239 * I am leaving this here just in case I am wrong
241 * if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
243 if (0)
245 WORKREQUEST workRequest;
246 struct WORKREQ_HTTPOPENREQUESTA *req;
248 workRequest.asyncall = HTTPOPENREQUESTA;
249 workRequest.handle = hHttpSession;
250 req = &workRequest.u.HttpOpenRequestA;
251 req->lpszVerb = HTTP_strdup(lpszVerb);
252 req->lpszObjectName = HTTP_strdup(lpszObjectName);
253 if (lpszVersion)
254 req->lpszVersion = HTTP_strdup(lpszVersion);
255 else
256 req->lpszVersion = 0;
257 if (lpszReferrer)
258 req->lpszReferrer = HTTP_strdup(lpszReferrer);
259 else
260 req->lpszReferrer = 0;
261 req->lpszAcceptTypes = lpszAcceptTypes;
262 req->dwFlags = dwFlags;
263 req->dwContext = dwContext;
265 INTERNET_AsyncCall(&workRequest);
266 TRACE ("returning NULL\n");
267 return NULL;
269 else
271 HINTERNET rec = HTTP_HttpOpenRequestA(hHttpSession, lpszVerb, lpszObjectName,
272 lpszVersion, lpszReferrer, lpszAcceptTypes,
273 dwFlags, dwContext);
274 TRACE("returning %p\n", rec);
275 return rec;
280 /***********************************************************************
281 * HttpOpenRequestW (WININET.@)
283 * Open a HTTP request handle
285 * RETURNS
286 * HINTERNET a HTTP request handle on success
287 * NULL on failure
289 * FIXME: This should be the other way around (A should call W)
291 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
292 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
293 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
294 DWORD dwFlags, DWORD dwContext)
296 CHAR *szVerb = NULL, *szObjectName = NULL;
297 CHAR *szVersion = NULL, *szReferrer = NULL, **szAcceptTypes = NULL;
298 INT len;
299 INT acceptTypesCount;
300 HINTERNET rc = FALSE;
301 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
302 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
303 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
304 dwFlags, dwContext);
306 if (lpszVerb)
308 len = WideCharToMultiByte(CP_ACP, 0, lpszVerb, -1, NULL, 0, NULL, NULL);
309 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR) );
310 if ( !szVerb )
311 goto end;
312 WideCharToMultiByte(CP_ACP, 0, lpszVerb, -1, szVerb, len, NULL, NULL);
315 if (lpszObjectName)
317 len = WideCharToMultiByte(CP_ACP, 0, lpszObjectName, -1, NULL, 0, NULL, NULL);
318 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR) );
319 if ( !szObjectName )
320 goto end;
321 WideCharToMultiByte(CP_ACP, 0, lpszObjectName, -1, szObjectName, len, NULL, NULL);
324 if (lpszVersion)
326 len = WideCharToMultiByte(CP_ACP, 0, lpszVersion, -1, NULL, 0, NULL, NULL);
327 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
328 if ( !szVersion )
329 goto end;
330 WideCharToMultiByte(CP_ACP, 0, lpszVersion, -1, szVersion, len, NULL, NULL);
333 if (lpszReferrer)
335 len = WideCharToMultiByte(CP_ACP, 0, lpszReferrer, -1, NULL, 0, NULL, NULL);
336 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
337 if ( !szReferrer )
338 goto end;
339 WideCharToMultiByte(CP_ACP, 0, lpszReferrer, -1, szReferrer, len, NULL, NULL);
342 acceptTypesCount = 0;
343 if (lpszAcceptTypes)
345 while (lpszAcceptTypes[acceptTypesCount]) { acceptTypesCount++; } /* find out how many there are */
346 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(CHAR *) * acceptTypesCount);
347 acceptTypesCount = 0;
348 while (lpszAcceptTypes[acceptTypesCount])
350 len = WideCharToMultiByte(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
351 -1, NULL, 0, NULL, NULL);
352 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
353 if (!szAcceptTypes[acceptTypesCount] )
354 goto end;
355 WideCharToMultiByte(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
356 -1, szAcceptTypes[acceptTypesCount], len, NULL, NULL);
357 acceptTypesCount++;
360 else szAcceptTypes = 0;
362 rc = HttpOpenRequestA(hHttpSession, (LPCSTR)szVerb, (LPCSTR)szObjectName,
363 (LPCSTR)szVersion, (LPCSTR)szReferrer,
364 (LPCSTR *)szAcceptTypes, dwFlags, dwContext);
366 end:
367 if (szAcceptTypes)
369 acceptTypesCount = 0;
370 while (szAcceptTypes[acceptTypesCount])
372 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
373 acceptTypesCount++;
375 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
377 if (szReferrer) HeapFree(GetProcessHeap(), 0, szReferrer);
378 if (szVersion) HeapFree(GetProcessHeap(), 0, szVersion);
379 if (szObjectName) HeapFree(GetProcessHeap(), 0, szObjectName);
380 if (szVerb) HeapFree(GetProcessHeap(), 0, szVerb);
382 return rc;
385 /***********************************************************************
386 * HTTP_Base64
388 static UINT HTTP_Base64( LPCSTR bin, LPSTR base64 )
390 UINT n = 0, x;
391 static LPSTR HTTP_Base64Enc =
392 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
394 while( bin[0] )
396 /* first 6 bits, all from bin[0] */
397 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
398 x = (bin[0] & 3) << 4;
400 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
401 if( !bin[1] )
403 base64[n++] = HTTP_Base64Enc[x];
404 base64[n++] = '=';
405 base64[n++] = '=';
406 break;
408 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
409 x = ( bin[1] & 0x0f ) << 2;
411 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
412 if( !bin[2] )
414 base64[n++] = HTTP_Base64Enc[x];
415 base64[n++] = '=';
416 break;
418 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
420 /* last 6 bits, all from bin [2] */
421 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
422 bin += 3;
424 base64[n] = 0;
425 return n;
428 /***********************************************************************
429 * HTTP_EncodeBasicAuth
431 * Encode the basic authentication string for HTTP 1.1
433 static LPSTR HTTP_EncodeBasicAuth( LPCSTR username, LPCSTR password)
435 UINT len;
436 LPSTR in, out, szBasic = "Basic ";
438 len = strlen( username ) + 1 + strlen ( password ) + 1;
439 in = HeapAlloc( GetProcessHeap(), 0, len );
440 if( !in )
441 return NULL;
443 len = strlen(szBasic) +
444 (strlen( username ) + 1 + strlen ( password ))*2 + 1 + 1;
445 out = HeapAlloc( GetProcessHeap(), 0, len );
446 if( out )
448 strcpy( in, username );
449 strcat( in, ":" );
450 strcat( in, password );
451 strcpy( out, szBasic );
452 HTTP_Base64( in, &out[strlen(out)] );
454 HeapFree( GetProcessHeap(), 0, in );
456 return out;
459 /***********************************************************************
460 * HTTP_InsertProxyAuthorization
462 * Insert the basic authorization field in the request header
464 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQA lpwhr,
465 LPCSTR username, LPCSTR password )
467 HTTPHEADERA hdr;
468 INT index;
470 hdr.lpszField = "Proxy-Authorization";
471 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
472 hdr.wFlags = HDR_ISREQUEST;
473 hdr.wCount = 0;
474 if( !hdr.lpszValue )
475 return FALSE;
477 TRACE("Inserting %s = %s\n",
478 debugstr_a( hdr.lpszField ), debugstr_a( hdr.lpszValue ) );
480 /* remove the old proxy authorization header */
481 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
482 if( index >=0 )
483 HTTP_DeleteCustomHeader( lpwhr, index );
485 HTTP_InsertCustomHeader(lpwhr, &hdr);
486 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
488 return TRUE;
491 /***********************************************************************
492 * HTTP_DealWithProxy
494 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOA hIC,
495 LPWININETHTTPSESSIONA lpwhs, LPWININETHTTPREQA lpwhr)
497 char buf[MAXHOSTNAME];
498 char proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
499 char* url, *szNul = "";
500 URL_COMPONENTSA UrlComponents;
502 memset( &UrlComponents, 0, sizeof UrlComponents );
503 UrlComponents.dwStructSize = sizeof UrlComponents;
504 UrlComponents.lpszHostName = buf;
505 UrlComponents.dwHostNameLength = MAXHOSTNAME;
507 sprintf(proxy, "http://%s/", hIC->lpszProxy);
508 if( !InternetCrackUrlA(proxy, 0, 0, &UrlComponents) )
509 return FALSE;
510 if( UrlComponents.dwHostNameLength == 0 )
511 return FALSE;
513 if( !lpwhr->lpszPath )
514 lpwhr->lpszPath = szNul;
515 TRACE("server='%s' path='%s'\n",
516 lpwhs->lpszServerName, lpwhr->lpszPath);
517 /* for constant 15 see above */
518 url = HeapAlloc(GetProcessHeap(), 0,
519 strlen(lpwhs->lpszServerName) + strlen(lpwhr->lpszPath) + 15);
521 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
522 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
524 sprintf(url, "http://%s:%d", lpwhs->lpszServerName,
525 lpwhs->nServerPort);
526 if( lpwhr->lpszPath[0] != '/' )
527 strcat( url, "/" );
528 strcat(url, lpwhr->lpszPath);
529 if(lpwhr->lpszPath != szNul)
530 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
531 lpwhr->lpszPath = url;
532 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
533 lpwhs->lpszServerName = HTTP_strdup(UrlComponents.lpszHostName);
534 lpwhs->nServerPort = UrlComponents.nPort;
536 return TRUE;
539 /***********************************************************************
540 * HTTP_HttpOpenRequestA (internal)
542 * Open a HTTP request handle
544 * RETURNS
545 * HINTERNET a HTTP request handle on success
546 * NULL on failure
549 HINTERNET WINAPI HTTP_HttpOpenRequestA(HINTERNET hHttpSession,
550 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
551 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
552 DWORD dwFlags, DWORD dwContext)
554 LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) hHttpSession;
555 LPWININETAPPINFOA hIC = NULL;
556 LPWININETHTTPREQA lpwhr;
557 LPSTR lpszCookies;
558 LPSTR lpszUrl = NULL;
559 DWORD nCookieSize;
561 TRACE("--> \n");
563 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
565 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
566 return FALSE;
569 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
571 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQA));
572 if (NULL == lpwhr)
574 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
575 return (HINTERNET) NULL;
578 lpwhr->hdr.htype = WH_HHTTPREQ;
579 lpwhr->hdr.lpwhparent = hHttpSession;
580 lpwhr->hdr.dwFlags = dwFlags;
581 lpwhr->hdr.dwContext = dwContext;
582 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
584 if (NULL != lpszObjectName && strlen(lpszObjectName)) {
585 DWORD needed = 0;
586 HRESULT rc;
587 rc = UrlEscapeA(lpszObjectName, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
588 if (rc != E_POINTER)
589 needed = strlen(lpszObjectName)+1;
590 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed);
591 rc = UrlEscapeA(lpszObjectName, lpwhr->lpszPath, &needed,
592 URL_ESCAPE_SPACES_ONLY);
593 if (rc)
595 ERR("Unable to escape string!(%s) (%ld)\n",lpszObjectName,rc);
596 strcpy(lpwhr->lpszPath,lpszObjectName);
600 if (NULL != lpszReferrer && strlen(lpszReferrer))
601 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
603 if(lpszAcceptTypes!=NULL)
605 int i;
606 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
607 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
610 if (NULL == lpszVerb)
611 lpwhr->lpszVerb = HTTP_strdup("GET");
612 else if (strlen(lpszVerb))
613 lpwhr->lpszVerb = HTTP_strdup(lpszVerb);
615 if (NULL != lpszReferrer && strlen(lpszReferrer))
617 char buf[MAXHOSTNAME];
618 URL_COMPONENTSA UrlComponents;
620 memset( &UrlComponents, 0, sizeof UrlComponents );
621 UrlComponents.dwStructSize = sizeof UrlComponents;
622 UrlComponents.lpszHostName = buf;
623 UrlComponents.dwHostNameLength = MAXHOSTNAME;
625 InternetCrackUrlA(lpszReferrer, 0, 0, &UrlComponents);
626 if (strlen(UrlComponents.lpszHostName))
627 lpwhr->lpszHostName = HTTP_strdup(UrlComponents.lpszHostName);
628 } else {
629 lpwhr->lpszHostName = HTTP_strdup(lpwhs->lpszServerName);
631 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
632 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
634 if (hIC->lpszAgent)
636 char *agent_header = HeapAlloc(GetProcessHeap(), 0, strlen(hIC->lpszAgent) + 1 + 14);
637 sprintf(agent_header, "User-Agent: %s\r\n", hIC->lpszAgent);
638 HttpAddRequestHeadersA((HINTERNET)lpwhr, agent_header, strlen(agent_header),
639 HTTP_ADDREQ_FLAG_ADD);
640 HeapFree(GetProcessHeap(), 0, agent_header);
643 lpszUrl = HeapAlloc(GetProcessHeap(), 0, strlen(lpwhr->lpszHostName) + 1 + 7);
644 sprintf(lpszUrl, "http://%s", lpwhr->lpszHostName);
645 if (InternetGetCookieA(lpszUrl, NULL, NULL, &nCookieSize))
647 int cnt = 0;
649 lpszCookies = HeapAlloc(GetProcessHeap(), 0, nCookieSize + 1 + 8);
651 cnt += sprintf(lpszCookies, "Cookie: ");
652 InternetGetCookieA(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
653 cnt += nCookieSize - 1;
654 sprintf(lpszCookies + cnt, "\r\n");
656 HttpAddRequestHeadersA((HINTERNET)lpwhr, lpszCookies, strlen(lpszCookies),
657 HTTP_ADDREQ_FLAG_ADD);
658 HeapFree(GetProcessHeap(), 0, lpszCookies);
660 HeapFree(GetProcessHeap(), 0, lpszUrl);
664 if (hIC->lpfnStatusCB)
666 INTERNET_ASYNC_RESULT iar;
668 iar.dwResult = (DWORD)lpwhr;
669 iar.dwError = ERROR_SUCCESS;
671 SendAsyncCallback(hIC, hHttpSession, dwContext,
672 INTERNET_STATUS_HANDLE_CREATED, &iar,
673 sizeof(INTERNET_ASYNC_RESULT));
677 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
681 * According to my tests. The name is not resolved until a request is Opened
683 SendAsyncCallback(hIC, hHttpSession, dwContext,
684 INTERNET_STATUS_RESOLVING_NAME,
685 lpwhs->lpszServerName,
686 strlen(lpwhs->lpszServerName)+1);
687 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
688 &lpwhs->phostent, &lpwhs->socketAddress))
690 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
691 return FALSE;
694 SendAsyncCallback(hIC, hHttpSession, lpwhr->hdr.dwContext,
695 INTERNET_STATUS_NAME_RESOLVED,
696 &(lpwhs->socketAddress),
697 sizeof(struct sockaddr_in));
699 TRACE("<--\n");
700 return (HINTERNET) lpwhr;
704 /***********************************************************************
705 * HttpQueryInfoA (WININET.@)
707 * Queries for information about an HTTP request
709 * RETURNS
710 * TRUE on success
711 * FALSE on failure
714 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
715 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
717 LPHTTPHEADERA lphttpHdr = NULL;
718 BOOL bSuccess = FALSE;
719 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
721 TRACE("(0x%08lx)--> %ld\n", dwInfoLevel, dwInfoLevel);
723 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
725 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
726 return FALSE;
729 /* Find requested header structure */
730 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
732 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPSTR)lpBuffer);
734 if (index < 0)
735 goto lend;
737 lphttpHdr = &lpwhr->pCustHeaders[index];
739 else
741 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
743 if (index == HTTP_QUERY_RAW_HEADERS_CRLF || index == HTTP_QUERY_RAW_HEADERS)
745 INT i, delim, size = 0, cnt = 0;
747 delim = index == HTTP_QUERY_RAW_HEADERS_CRLF ? 2 : 1;
749 /* Calculate length of custom reuqest headers */
750 for (i = 0; i < lpwhr->nCustHeaders; i++)
752 if ((~lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST) && lpwhr->pCustHeaders[i].lpszField &&
753 lpwhr->pCustHeaders[i].lpszValue)
755 size += strlen(lpwhr->pCustHeaders[i].lpszField) +
756 strlen(lpwhr->pCustHeaders[i].lpszValue) + delim + 2;
760 /* Calculate the length of stadard request headers */
761 for (i = 0; i <= HTTP_QUERY_MAX; i++)
763 if ((~lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST) && lpwhr->StdHeaders[i].lpszField &&
764 lpwhr->StdHeaders[i].lpszValue)
766 size += strlen(lpwhr->StdHeaders[i].lpszField) +
767 strlen(lpwhr->StdHeaders[i].lpszValue) + delim + 2;
770 size += delim;
772 if (size + 1 > *lpdwBufferLength)
774 *lpdwBufferLength = size + 1;
775 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
776 goto lend;
779 /* Append standard request heades */
780 for (i = 0; i <= HTTP_QUERY_MAX; i++)
782 if ((~lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST) &&
783 lpwhr->StdHeaders[i].lpszField &&
784 lpwhr->StdHeaders[i].lpszValue)
786 cnt += sprintf((char*)lpBuffer + cnt, "%s: %s%s", lpwhr->StdHeaders[i].lpszField, lpwhr->StdHeaders[i].lpszValue,
787 index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "\0");
791 /* Append custom request heades */
792 for (i = 0; i < lpwhr->nCustHeaders; i++)
794 if ((~lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST) &&
795 lpwhr->pCustHeaders[i].lpszField &&
796 lpwhr->pCustHeaders[i].lpszValue)
798 cnt += sprintf((char*)lpBuffer + cnt, "%s: %s%s",
799 lpwhr->pCustHeaders[i].lpszField, lpwhr->pCustHeaders[i].lpszValue,
800 index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "\0");
804 strcpy((char*)lpBuffer + cnt, index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "");
806 *lpdwBufferLength = cnt + delim;
807 bSuccess = TRUE;
808 goto lend;
810 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
812 lphttpHdr = &lpwhr->StdHeaders[index];
814 else
815 goto lend;
818 /* Ensure header satisifies requested attributes */
819 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
820 (~lphttpHdr->wFlags & HDR_ISREQUEST))
821 goto lend;
823 /* coalesce value to reuqested type */
824 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
826 *(int *)lpBuffer = atoi(lphttpHdr->lpszValue);
827 bSuccess = TRUE;
829 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
831 time_t tmpTime;
832 struct tm tmpTM;
833 SYSTEMTIME *STHook;
835 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
837 tmpTM = *gmtime(&tmpTime);
838 STHook = (SYSTEMTIME *) lpBuffer;
839 if(STHook==NULL)
840 goto lend;
842 STHook->wDay = tmpTM.tm_mday;
843 STHook->wHour = tmpTM.tm_hour;
844 STHook->wMilliseconds = 0;
845 STHook->wMinute = tmpTM.tm_min;
846 STHook->wDayOfWeek = tmpTM.tm_wday;
847 STHook->wMonth = tmpTM.tm_mon + 1;
848 STHook->wSecond = tmpTM.tm_sec;
849 STHook->wYear = tmpTM.tm_year;
851 bSuccess = TRUE;
853 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
855 if (*lpdwIndex >= lphttpHdr->wCount)
857 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
859 else
861 /* Copy strncpy(lpBuffer, lphttpHdr[*lpdwIndex], len); */
862 (*lpdwIndex)++;
865 else
867 INT len = strlen(lphttpHdr->lpszValue);
869 if (len + 1 > *lpdwBufferLength)
871 *lpdwBufferLength = len + 1;
872 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
873 goto lend;
876 strncpy(lpBuffer, lphttpHdr->lpszValue, len);
877 ((char*)lpBuffer)[len]=0;
878 *lpdwBufferLength = len;
879 bSuccess = TRUE;
882 lend:
883 TRACE("%d <--\n", bSuccess);
884 return bSuccess;
887 /***********************************************************************
888 * HttpQueryInfoW (WININET.@)
890 * Queries for information about an HTTP request
892 * RETURNS
893 * TRUE on success
894 * FALSE on failure
897 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
898 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
900 BOOL result;
901 DWORD charLen=*lpdwBufferLength;
902 char* tempBuffer=HeapAlloc(GetProcessHeap(), 0, charLen);
903 result=HttpQueryInfoA(hHttpRequest, dwInfoLevel, tempBuffer, &charLen, lpdwIndex);
904 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
905 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
907 memcpy(lpBuffer,tempBuffer,charLen);
909 else
911 int nChars=MultiByteToWideChar(CP_ACP,0, tempBuffer,charLen,lpBuffer,*lpdwBufferLength);
912 *lpdwBufferLength=nChars;
914 HeapFree(GetProcessHeap(), 0, tempBuffer);
915 return result;
918 /***********************************************************************
919 * HttpSendRequestExA (WININET.@)
921 * Sends the specified request to the HTTP server and allows chunked
922 * transfers
924 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
925 LPINTERNET_BUFFERSA lpBuffersIn,
926 LPINTERNET_BUFFERSA lpBuffersOut,
927 DWORD dwFlags, DWORD dwContext)
929 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
930 lpBuffersOut, dwFlags, dwContext);
931 return FALSE;
934 /***********************************************************************
935 * HttpSendRequestA (WININET.@)
937 * Sends the specified request to the HTTP server
939 * RETURNS
940 * TRUE on success
941 * FALSE on failure
944 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
945 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
947 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
948 LPWININETHTTPSESSIONA lpwhs = NULL;
949 LPWININETAPPINFOA hIC = NULL;
951 TRACE("(0x%08lx, %p (%s), %li, %p, %li)\n", (unsigned long)hHttpRequest,
952 lpszHeaders, debugstr_a(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
954 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
956 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
957 return FALSE;
960 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
961 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
963 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
964 return FALSE;
967 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
968 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
970 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
971 return FALSE;
974 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
976 WORKREQUEST workRequest;
977 struct WORKREQ_HTTPSENDREQUESTA *req;
979 workRequest.asyncall = HTTPSENDREQUESTA;
980 workRequest.handle = hHttpRequest;
981 req = &workRequest.u.HttpSendRequestA;
982 if (lpszHeaders)
983 req->lpszHeader = HTTP_strdup(lpszHeaders);
984 else
985 req->lpszHeader = 0;
986 req->dwHeaderLength = dwHeaderLength;
987 req->lpOptional = lpOptional;
988 req->dwOptionalLength = dwOptionalLength;
990 INTERNET_AsyncCall(&workRequest);
992 * This is from windows.
994 SetLastError(ERROR_IO_PENDING);
995 return 0;
997 else
999 return HTTP_HttpSendRequestA(hHttpRequest, lpszHeaders,
1000 dwHeaderLength, lpOptional, dwOptionalLength);
1004 /***********************************************************************
1005 * HttpSendRequestW (WININET.@)
1007 * Sends the specified request to the HTTP server
1009 * RETURNS
1010 * TRUE on success
1011 * FALSE on failure
1014 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1015 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1017 BOOL result;
1018 char* szHeaders=NULL;
1019 DWORD nLen=dwHeaderLength;
1020 if(lpszHeaders!=NULL)
1022 nLen=WideCharToMultiByte(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0,NULL,NULL);
1023 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen);
1024 WideCharToMultiByte(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen,NULL,NULL);
1026 result=HttpSendRequestA(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1027 if(szHeaders!=NULL)
1028 HeapFree(GetProcessHeap(),0,szHeaders);
1029 return result;
1032 /***********************************************************************
1033 * HTTP_HandleRedirect (internal)
1035 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQA lpwhr, LPCSTR lpszUrl, LPCSTR lpszHeaders,
1036 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1038 LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
1039 LPWININETAPPINFOA hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
1040 char path[2048];
1041 if(lpszUrl[0]=='/')
1043 /* if it's an absolute path, keep the same session info */
1044 strcpy(path,lpszUrl);
1046 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1048 TRACE("Redirect through proxy\n");
1049 strcpy(path,lpszUrl);
1051 else
1053 URL_COMPONENTSA urlComponents;
1054 char protocol[32], hostName[MAXHOSTNAME], userName[1024];
1055 char password[1024], extra[1024];
1056 urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
1057 urlComponents.lpszScheme = protocol;
1058 urlComponents.dwSchemeLength = 32;
1059 urlComponents.lpszHostName = hostName;
1060 urlComponents.dwHostNameLength = MAXHOSTNAME;
1061 urlComponents.lpszUserName = userName;
1062 urlComponents.dwUserNameLength = 1024;
1063 urlComponents.lpszPassword = password;
1064 urlComponents.dwPasswordLength = 1024;
1065 urlComponents.lpszUrlPath = path;
1066 urlComponents.dwUrlPathLength = 2048;
1067 urlComponents.lpszExtraInfo = extra;
1068 urlComponents.dwExtraInfoLength = 1024;
1069 if(!InternetCrackUrlA(lpszUrl, strlen(lpszUrl), 0, &urlComponents))
1070 return FALSE;
1072 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1073 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1075 #if 0
1077 * This upsets redirects to binary files on sourceforge.net
1078 * and gives an html page instead of the target file
1079 * Examination of the HTTP request sent by native wininet.dll
1080 * reveals that it doesn't send a referrer in that case.
1081 * Maybe there's a flag that enables this, or maybe a referrer
1082 * shouldn't be added in case of a redirect.
1085 /* consider the current host as the referrer */
1086 if (NULL != lpwhs->lpszServerName && strlen(lpwhs->lpszServerName))
1087 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1088 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1089 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1090 #endif
1092 if (NULL != lpwhs->lpszServerName)
1093 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1094 lpwhs->lpszServerName = HTTP_strdup(hostName);
1095 if (NULL != lpwhs->lpszUserName)
1096 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1097 lpwhs->lpszUserName = HTTP_strdup(userName);
1098 lpwhs->nServerPort = urlComponents.nPort;
1100 if (NULL != lpwhr->lpszHostName)
1101 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1102 lpwhr->lpszHostName=HTTP_strdup(hostName);
1104 SendAsyncCallback(hIC, lpwhs, lpwhr->hdr.dwContext,
1105 INTERNET_STATUS_RESOLVING_NAME,
1106 lpwhs->lpszServerName,
1107 strlen(lpwhs->lpszServerName)+1);
1109 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1110 &lpwhs->phostent, &lpwhs->socketAddress))
1112 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1113 return FALSE;
1116 SendAsyncCallback(hIC, lpwhs, lpwhr->hdr.dwContext,
1117 INTERNET_STATUS_NAME_RESOLVED,
1118 &(lpwhs->socketAddress),
1119 sizeof(struct sockaddr_in));
1123 if(lpwhr->lpszPath)
1124 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1125 lpwhr->lpszPath=NULL;
1126 if (strlen(path))
1128 DWORD needed = 0;
1129 HRESULT rc;
1130 rc = UrlEscapeA(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1131 if (rc != E_POINTER)
1132 needed = strlen(path)+1;
1133 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed);
1134 rc = UrlEscapeA(path, lpwhr->lpszPath, &needed,
1135 URL_ESCAPE_SPACES_ONLY);
1136 if (rc)
1138 ERR("Unable to escape string!(%s) (%ld)\n",path,rc);
1139 strcpy(lpwhr->lpszPath,path);
1143 return HttpSendRequestA((HINTERNET)lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1146 /***********************************************************************
1147 * HTTP_HttpSendRequestA (internal)
1149 * Sends the specified request to the HTTP server
1151 * RETURNS
1152 * TRUE on success
1153 * FALSE on failure
1156 BOOL WINAPI HTTP_HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1157 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1159 INT cnt;
1160 INT i;
1161 BOOL bSuccess = FALSE;
1162 LPSTR requestString = NULL;
1163 LPSTR lpszHeaders_r_n = NULL; /* lpszHeaders with atleast one pair of \r\n at the end */
1164 INT requestStringLen;
1165 INT responseLen;
1166 INT headerLength = 0;
1167 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
1168 LPWININETHTTPSESSIONA lpwhs = NULL;
1169 LPWININETAPPINFOA hIC = NULL;
1170 BOOL loop_next = FALSE;
1171 int CustHeaderIndex;
1173 TRACE("--> 0x%08lx\n", (ULONG)hHttpRequest);
1175 /* Verify our tree of internet handles */
1176 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1178 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1179 return FALSE;
1182 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
1183 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1185 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1186 return FALSE;
1189 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
1190 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1192 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1193 return FALSE;
1196 /* Clear any error information */
1197 INTERNET_SetLastError(0);
1200 /* We must have a verb */
1201 if (NULL == lpwhr->lpszVerb)
1203 goto lend;
1206 /* if we are using optional stuff, we must add the fixed header of that option length */
1207 if (lpOptional && dwOptionalLength)
1209 char contentLengthStr[sizeof("Content-Length: ") + 20 /* int */ + 2 /* \n\r */];
1210 sprintf(contentLengthStr, "Content-Length: %li\r\n", dwOptionalLength);
1211 HttpAddRequestHeadersA(hHttpRequest, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1216 TRACE("Going to url %s %s\n", debugstr_a(lpwhr->lpszHostName), debugstr_a(lpwhr->lpszPath));
1217 loop_next = FALSE;
1219 /* If we don't have a path we set it to root */
1220 if (NULL == lpwhr->lpszPath)
1221 lpwhr->lpszPath = HTTP_strdup("/");
1223 if(strncmp(lpwhr->lpszPath, "http://", sizeof("http://") -1) != 0
1224 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1226 char *fixurl = HeapAlloc(GetProcessHeap(), 0, strlen(lpwhr->lpszPath) + 2);
1227 *fixurl = '/';
1228 strcpy(fixurl + 1, lpwhr->lpszPath);
1229 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1230 lpwhr->lpszPath = fixurl;
1233 /* Calculate length of request string */
1234 requestStringLen =
1235 strlen(lpwhr->lpszVerb) +
1236 strlen(lpwhr->lpszPath) +
1237 strlen(HTTPHEADER) +
1238 5; /* " \r\n\r\n" */
1240 /* add "\r\n" to end of lpszHeaders if needed */
1241 if (lpszHeaders)
1243 int len = strlen(lpszHeaders);
1245 /* Check if the string is terminated with \r\n, but not if
1246 * the string is less that 2 characters long, because then
1247 * we would be looking at memory before the beginning of
1248 * the string. Besides, if it is less than 2 characters
1249 * long, then clearly, its not terminated with \r\n.
1251 if ((len > 2) && (memcmp(lpszHeaders + (len - 2), "\r\n", 2) == 0))
1253 lpszHeaders_r_n = HTTP_strdup(lpszHeaders);
1255 else
1257 TRACE("Adding \r\n to lpszHeaders.\n");
1258 lpszHeaders_r_n = HeapAlloc( GetProcessHeap(), 0, strlen(lpszHeaders) + 3 );
1259 strcpy( lpszHeaders_r_n, lpszHeaders );
1260 strcpy( lpszHeaders_r_n + strlen(lpszHeaders), "\r\n" );
1264 /* Add length of passed headers */
1265 if (lpszHeaders)
1267 headerLength = -1 == dwHeaderLength ? strlen(lpszHeaders_r_n) : dwHeaderLength;
1268 requestStringLen += headerLength + 2; /* \r\n */
1272 /* if there isa proxy username and password, add it to the headers */
1273 if( hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ) )
1275 HTTP_InsertProxyAuthorization( lpwhr, hIC->lpszProxyUsername, hIC->lpszProxyPassword );
1278 /* Calculate length of custom request headers */
1279 for (i = 0; i < lpwhr->nCustHeaders; i++)
1281 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1283 requestStringLen += strlen(lpwhr->pCustHeaders[i].lpszField) +
1284 strlen(lpwhr->pCustHeaders[i].lpszValue) + 4; /*: \r\n */
1288 /* Calculate the length of standard request headers */
1289 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1291 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1293 requestStringLen += strlen(lpwhr->StdHeaders[i].lpszField) +
1294 strlen(lpwhr->StdHeaders[i].lpszValue) + 4; /*: \r\n */
1298 if (lpwhr->lpszHostName)
1299 requestStringLen += (strlen(HTTPHOSTHEADER) + strlen(lpwhr->lpszHostName));
1301 /* if there is optional data to send, add the length */
1302 if (lpOptional)
1304 requestStringLen += dwOptionalLength;
1307 /* Allocate string to hold entire request */
1308 requestString = HeapAlloc(GetProcessHeap(), 0, requestStringLen + 1);
1309 if (NULL == requestString)
1311 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1312 goto lend;
1315 /* Build request string */
1316 cnt = sprintf(requestString, "%s %s%s",
1317 lpwhr->lpszVerb,
1318 lpwhr->lpszPath,
1319 HTTPHEADER);
1321 /* Append standard request headers */
1322 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1324 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1326 cnt += sprintf(requestString + cnt, "\r\n%s: %s",
1327 lpwhr->StdHeaders[i].lpszField, lpwhr->StdHeaders[i].lpszValue);
1328 TRACE("Adding header %s (%s)\n",lpwhr->StdHeaders[i].lpszField,lpwhr->StdHeaders[i].lpszValue);
1332 /* Append custom request heades */
1333 for (i = 0; i < lpwhr->nCustHeaders; i++)
1335 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1337 cnt += sprintf(requestString + cnt, "\r\n%s: %s",
1338 lpwhr->pCustHeaders[i].lpszField, lpwhr->pCustHeaders[i].lpszValue);
1339 TRACE("Adding custom header %s (%s)\n",lpwhr->pCustHeaders[i].lpszField,lpwhr->pCustHeaders[i].lpszValue);
1343 if (lpwhr->lpszHostName)
1344 cnt += sprintf(requestString + cnt, "%s%s", HTTPHOSTHEADER, lpwhr->lpszHostName);
1346 /* Append passed request headers */
1347 if (lpszHeaders_r_n)
1349 strcpy(requestString + cnt, "\r\n");
1350 cnt += 2;
1351 strcpy(requestString + cnt, lpszHeaders_r_n);
1352 cnt += headerLength;
1353 /* only add \r\n if not already present */
1354 if (memcmp((requestString + cnt) - 2, "\r\n", 2) != 0)
1356 strcpy(requestString + cnt, "\r\n");
1357 cnt += 2;
1361 /* Set (header) termination string for request */
1362 if (memcmp((requestString + cnt) - 4, "\r\n\r\n", 4) != 0)
1363 { /* only add it if the request string doesn't already
1364 have the thing.. (could happen if the custom header
1365 added it */
1366 strcpy(requestString + cnt, "\r\n");
1367 cnt += 2;
1369 else
1370 requestStringLen -= 2;
1372 /* if optional data, append it */
1373 if (lpOptional)
1375 memcpy(requestString + cnt, lpOptional, dwOptionalLength);
1376 cnt += dwOptionalLength;
1377 /* we also have to decrease the expected string length by two,
1378 * since we won't be adding on those following \r\n's */
1379 requestStringLen -= 2;
1381 else
1382 { /* if there is no optional data, add on another \r\n just to be safe */
1383 /* termination for request */
1384 strcpy(requestString + cnt, "\r\n");
1385 cnt += 2;
1388 TRACE("(%s) len(%d)\n", requestString, requestStringLen);
1389 /* Send the request and store the results */
1390 if (!HTTP_OpenConnection(lpwhr))
1391 goto lend;
1393 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1394 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1396 NETCON_send(&lpwhr->netConnection, requestString, requestStringLen,
1397 0, &cnt);
1400 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1401 INTERNET_STATUS_REQUEST_SENT,
1402 &requestStringLen,sizeof(DWORD));
1404 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1405 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1407 if (cnt < 0)
1408 goto lend;
1410 responseLen = HTTP_GetResponseHeaders(lpwhr);
1411 if (responseLen)
1412 bSuccess = TRUE;
1414 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1415 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1416 sizeof(DWORD));
1418 /* process headers here. Is this right? */
1419 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, "Set-Cookie");
1420 if (CustHeaderIndex >= 0)
1422 LPHTTPHEADERA setCookieHeader;
1423 int nPosStart = 0, nPosEnd = 0;
1425 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1427 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1429 LPSTR buf_cookie, cookie_name, cookie_data;
1430 LPSTR buf_url;
1431 LPSTR domain = NULL;
1432 int nEqualPos = 0;
1433 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1434 setCookieHeader->lpszValue[nPosEnd] != '\0')
1436 nPosEnd++;
1438 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1440 /* fixme: not case sensitive, strcasestr is gnu only */
1441 int nDomainPosEnd = 0;
1442 int nDomainPosStart = 0, nDomainLength = 0;
1443 LPSTR lpszDomain = strstr(&setCookieHeader->lpszValue[nPosEnd], "domain=");
1444 if (lpszDomain)
1445 { /* they have specified their own domain, lets use it */
1446 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1447 lpszDomain[nDomainPosEnd] != '\0')
1449 nDomainPosEnd++;
1451 nDomainPosStart = strlen("domain=");
1452 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1453 domain = HeapAlloc(GetProcessHeap(), 0, nDomainLength + 1);
1454 strncpy(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1455 domain[nDomainLength] = '\0';
1458 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1459 buf_cookie = HeapAlloc(GetProcessHeap(), 0, (nPosEnd - nPosStart) + 1);
1460 strncpy(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1461 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1462 TRACE("%s\n", buf_cookie);
1463 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1465 nEqualPos++;
1467 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1469 HeapFree(GetProcessHeap(), 0, buf_cookie);
1470 break;
1473 cookie_name = HeapAlloc(GetProcessHeap(), 0, nEqualPos + 1);
1474 strncpy(cookie_name, buf_cookie, nEqualPos);
1475 cookie_name[nEqualPos] = '\0';
1476 cookie_data = &buf_cookie[nEqualPos + 1];
1479 buf_url = HeapAlloc(GetProcessHeap(), 0, strlen((domain ? domain : lpwhr->lpszHostName)) + strlen(lpwhr->lpszPath) + 9);
1480 sprintf(buf_url, "http://%s/", (domain ? domain : lpwhr->lpszHostName)); /* FIXME PATH!!! */
1481 InternetSetCookieA(buf_url, cookie_name, cookie_data);
1483 HeapFree(GetProcessHeap(), 0, buf_url);
1484 HeapFree(GetProcessHeap(), 0, buf_cookie);
1485 HeapFree(GetProcessHeap(), 0, cookie_name);
1486 if (domain) HeapFree(GetProcessHeap(), 0, domain);
1487 nPosStart = nPosEnd;
1491 while (loop_next);
1493 lend:
1495 if (requestString)
1496 HeapFree(GetProcessHeap(), 0, requestString);
1498 if (lpszHeaders)
1499 HeapFree(GetProcessHeap(), 0, lpszHeaders_r_n);
1501 /* TODO: send notification for P3P header */
1503 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1505 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1506 if(HttpQueryInfoA(hHttpRequest,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1507 (dwCode==302 || dwCode==301))
1509 char szNewLocation[2048];
1510 DWORD dwBufferSize=2048;
1511 dwIndex=0;
1512 if(HttpQueryInfoA(hHttpRequest,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1514 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1515 INTERNET_STATUS_REDIRECT, szNewLocation,
1516 dwBufferSize);
1517 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1518 dwHeaderLength, lpOptional, dwOptionalLength);
1523 if (hIC->lpfnStatusCB)
1525 INTERNET_ASYNC_RESULT iar;
1527 iar.dwResult = (DWORD)bSuccess;
1528 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1530 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1531 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1532 sizeof(INTERNET_ASYNC_RESULT));
1535 TRACE("<--\n");
1536 return bSuccess;
1540 /***********************************************************************
1541 * HTTP_Connect (internal)
1543 * Create http session handle
1545 * RETURNS
1546 * HINTERNET a session handle on success
1547 * NULL on failure
1550 HINTERNET HTTP_Connect(HINTERNET hInternet, LPCSTR lpszServerName,
1551 INTERNET_PORT nServerPort, LPCSTR lpszUserName,
1552 LPCSTR lpszPassword, DWORD dwFlags, DWORD dwContext)
1554 BOOL bSuccess = FALSE;
1555 LPWININETAPPINFOA hIC = NULL;
1556 LPWININETHTTPSESSIONA lpwhs = NULL;
1558 TRACE("-->\n");
1560 if (((LPWININETHANDLEHEADER)hInternet)->htype != WH_HINIT)
1561 goto lerror;
1563 hIC = (LPWININETAPPINFOA) hInternet;
1564 hIC->hdr.dwContext = dwContext;
1566 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONA));
1567 if (NULL == lpwhs)
1569 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1570 goto lerror;
1574 * According to my tests. The name is not resolved until a request is sent
1577 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1578 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1580 lpwhs->hdr.htype = WH_HHTTPSESSION;
1581 lpwhs->hdr.lpwhparent = (LPWININETHANDLEHEADER)hInternet;
1582 lpwhs->hdr.dwFlags = dwFlags;
1583 lpwhs->hdr.dwContext = dwContext;
1584 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1585 if(strchr(hIC->lpszProxy, ' '))
1586 FIXME("Several proxies not implemented.\n");
1587 if(hIC->lpszProxyBypass)
1588 FIXME("Proxy bypass is ignored.\n");
1590 if (NULL != lpszServerName)
1591 lpwhs->lpszServerName = HTTP_strdup(lpszServerName);
1592 if (NULL != lpszUserName)
1593 lpwhs->lpszUserName = HTTP_strdup(lpszUserName);
1594 lpwhs->nServerPort = nServerPort;
1596 if (hIC->lpfnStatusCB)
1598 INTERNET_ASYNC_RESULT iar;
1600 iar.dwResult = (DWORD)lpwhs;
1601 iar.dwError = ERROR_SUCCESS;
1603 SendAsyncCallback(hIC, hInternet, dwContext,
1604 INTERNET_STATUS_HANDLE_CREATED, &iar,
1605 sizeof(INTERNET_ASYNC_RESULT));
1608 bSuccess = TRUE;
1610 lerror:
1611 if (!bSuccess && lpwhs)
1613 HeapFree(GetProcessHeap(), 0, lpwhs);
1614 lpwhs = NULL;
1618 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1619 * windows
1622 TRACE("%p -->\n", hInternet);
1623 return (HINTERNET)lpwhs;
1627 /***********************************************************************
1628 * HTTP_OpenConnection (internal)
1630 * Connect to a web server
1632 * RETURNS
1634 * TRUE on success
1635 * FALSE on failure
1637 BOOL HTTP_OpenConnection(LPWININETHTTPREQA lpwhr)
1639 BOOL bSuccess = FALSE;
1640 LPWININETHTTPSESSIONA lpwhs;
1641 LPWININETAPPINFOA hIC = NULL;
1643 TRACE("-->\n");
1646 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1648 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1649 goto lend;
1652 lpwhs = (LPWININETHTTPSESSIONA)lpwhr->hdr.lpwhparent;
1654 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
1655 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1656 INTERNET_STATUS_CONNECTING_TO_SERVER,
1657 &(lpwhs->socketAddress),
1658 sizeof(struct sockaddr_in));
1660 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1661 SOCK_STREAM, 0))
1663 WARN("Socket creation failed\n");
1664 goto lend;
1667 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1668 sizeof(lpwhs->socketAddress)))
1670 WARN("Unable to connect to host (%s)\n", strerror(errno));
1671 goto lend;
1674 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1675 INTERNET_STATUS_CONNECTED_TO_SERVER,
1676 &(lpwhs->socketAddress),
1677 sizeof(struct sockaddr_in));
1679 bSuccess = TRUE;
1681 lend:
1682 TRACE("%d <--\n", bSuccess);
1683 return bSuccess;
1687 /***********************************************************************
1688 * HTTP_GetResponseHeaders (internal)
1690 * Read server response
1692 * RETURNS
1694 * TRUE on success
1695 * FALSE on error
1697 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr)
1699 INT cbreaks = 0;
1700 CHAR buffer[MAX_REPLY_LEN];
1701 DWORD buflen = MAX_REPLY_LEN;
1702 BOOL bSuccess = FALSE;
1703 INT rc = 0;
1704 CHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
1706 TRACE("-->\n");
1708 if (!NETCON_connected(&lpwhr->netConnection))
1709 goto lend;
1712 * HACK peek at the buffer
1714 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
1717 * We should first receive 'HTTP/1.x nnn' where nnn is the status code.
1719 buflen = MAX_REPLY_LEN;
1720 memset(buffer, 0, MAX_REPLY_LEN);
1721 if (!NETCON_getNextLine(&lpwhr->netConnection, buffer, &buflen))
1722 goto lend;
1724 if (strncmp(buffer, "HTTP", 4) != 0)
1725 goto lend;
1727 buffer[12]='\0';
1728 HTTP_ProcessHeader(lpwhr, "Status", buffer+9, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
1730 /* Parse each response line */
1733 buflen = MAX_REPLY_LEN;
1734 if (NETCON_getNextLine(&lpwhr->netConnection, buffer, &buflen))
1736 TRACE("got line %s, now interpretting\n", debugstr_a(buffer));
1737 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
1738 break;
1740 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
1742 else
1744 cbreaks++;
1745 if (cbreaks >= 2)
1746 break;
1748 }while(1);
1750 bSuccess = TRUE;
1752 lend:
1754 TRACE("<--\n");
1755 if (bSuccess)
1756 return rc;
1757 else
1758 return FALSE;
1762 /***********************************************************************
1763 * HTTP_InterpretHttpHeader (internal)
1765 * Parse server response
1767 * RETURNS
1769 * TRUE on success
1770 * FALSE on error
1772 INT stripSpaces(LPCSTR lpszSrc, LPSTR lpszStart, INT *len)
1774 LPCSTR lpsztmp;
1775 INT srclen;
1777 srclen = 0;
1779 while (*lpszSrc == ' ' && *lpszSrc != '\0')
1780 lpszSrc++;
1782 lpsztmp = lpszSrc;
1783 while(*lpsztmp != '\0')
1785 if (*lpsztmp != ' ')
1786 srclen = lpsztmp - lpszSrc + 1;
1788 lpsztmp++;
1791 *len = min(*len, srclen);
1792 strncpy(lpszStart, lpszSrc, *len);
1793 lpszStart[*len] = '\0';
1795 return *len;
1799 BOOL HTTP_InterpretHttpHeader(LPSTR buffer, LPSTR field, INT fieldlen, LPSTR value, INT valuelen)
1801 CHAR *pd;
1802 BOOL bSuccess = FALSE;
1804 TRACE("\n");
1806 *field = '\0';
1807 *value = '\0';
1809 pd = strchr(buffer, ':');
1810 if (pd)
1812 *pd = '\0';
1813 if (stripSpaces(buffer, field, &fieldlen) > 0)
1815 if (stripSpaces(pd+1, value, &valuelen) > 0)
1816 bSuccess = TRUE;
1820 TRACE("%d: field(%s) Value(%s)\n", bSuccess, field, value);
1821 return bSuccess;
1825 /***********************************************************************
1826 * HTTP_GetStdHeaderIndex (internal)
1828 * Lookup field index in standard http header array
1830 * FIXME: This should be stuffed into a hash table
1832 INT HTTP_GetStdHeaderIndex(LPCSTR lpszField)
1834 INT index = -1;
1836 if (!strcasecmp(lpszField, "Content-Length"))
1837 index = HTTP_QUERY_CONTENT_LENGTH;
1838 else if (!strcasecmp(lpszField,"Status"))
1839 index = HTTP_QUERY_STATUS_CODE;
1840 else if (!strcasecmp(lpszField,"Content-Type"))
1841 index = HTTP_QUERY_CONTENT_TYPE;
1842 else if (!strcasecmp(lpszField,"Last-Modified"))
1843 index = HTTP_QUERY_LAST_MODIFIED;
1844 else if (!strcasecmp(lpszField,"Location"))
1845 index = HTTP_QUERY_LOCATION;
1846 else if (!strcasecmp(lpszField,"Accept"))
1847 index = HTTP_QUERY_ACCEPT;
1848 else if (!strcasecmp(lpszField,"Referer"))
1849 index = HTTP_QUERY_REFERER;
1850 else if (!strcasecmp(lpszField,"Content-Transfer-Encoding"))
1851 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
1852 else if (!strcasecmp(lpszField,"Date"))
1853 index = HTTP_QUERY_DATE;
1854 else if (!strcasecmp(lpszField,"Server"))
1855 index = HTTP_QUERY_SERVER;
1856 else if (!strcasecmp(lpszField,"Connection"))
1857 index = HTTP_QUERY_CONNECTION;
1858 else if (!strcasecmp(lpszField,"ETag"))
1859 index = HTTP_QUERY_ETAG;
1860 else if (!strcasecmp(lpszField,"Accept-Ranges"))
1861 index = HTTP_QUERY_ACCEPT_RANGES;
1862 else if (!strcasecmp(lpszField,"Expires"))
1863 index = HTTP_QUERY_EXPIRES;
1864 else if (!strcasecmp(lpszField,"Mime-Version"))
1865 index = HTTP_QUERY_MIME_VERSION;
1866 else if (!strcasecmp(lpszField,"Pragma"))
1867 index = HTTP_QUERY_PRAGMA;
1868 else if (!strcasecmp(lpszField,"Cache-Control"))
1869 index = HTTP_QUERY_CACHE_CONTROL;
1870 else if (!strcasecmp(lpszField,"Content-Length"))
1871 index = HTTP_QUERY_CONTENT_LENGTH;
1872 else if (!strcasecmp(lpszField,"User-Agent"))
1873 index = HTTP_QUERY_USER_AGENT;
1874 else if (!strcasecmp(lpszField,"Proxy-Authenticate"))
1875 index = HTTP_QUERY_PROXY_AUTHENTICATE;
1876 else
1878 TRACE("Couldn't find %s in standard header table\n", lpszField);
1881 return index;
1885 /***********************************************************************
1886 * HTTP_ProcessHeader (internal)
1888 * Stuff header into header tables according to <dwModifier>
1892 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1894 BOOL HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr, LPCSTR field, LPCSTR value, DWORD dwModifier)
1896 LPHTTPHEADERA lphttpHdr = NULL;
1897 BOOL bSuccess = FALSE;
1898 INT index;
1900 TRACE("--> %s: %s - 0x%08x\n", field, value, (unsigned int)dwModifier);
1902 /* Adjust modifier flags */
1903 if (dwModifier & COALESCEFLASG)
1904 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
1906 /* Try to get index into standard header array */
1907 index = HTTP_GetStdHeaderIndex(field);
1908 if (index >= 0)
1910 lphttpHdr = &lpwhr->StdHeaders[index];
1912 else /* Find or create new custom header */
1914 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
1915 if (index >= 0)
1917 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
1919 return FALSE;
1921 lphttpHdr = &lpwhr->pCustHeaders[index];
1923 else
1925 HTTPHEADERA hdr;
1927 hdr.lpszField = (LPSTR)field;
1928 hdr.lpszValue = (LPSTR)value;
1929 hdr.wFlags = hdr.wCount = 0;
1931 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1932 hdr.wFlags |= HDR_ISREQUEST;
1934 return HTTP_InsertCustomHeader(lpwhr, &hdr);
1938 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1939 lphttpHdr->wFlags |= HDR_ISREQUEST;
1940 else
1941 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
1943 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
1945 INT slen;
1947 if (!lpwhr->StdHeaders[index].lpszField)
1949 lphttpHdr->lpszField = HTTP_strdup(field);
1951 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1952 lphttpHdr->wFlags |= HDR_ISREQUEST;
1955 slen = strlen(value) + 1;
1956 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen);
1957 if (lphttpHdr->lpszValue)
1959 memcpy(lphttpHdr->lpszValue, value, slen);
1960 bSuccess = TRUE;
1962 else
1964 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1967 else if (lphttpHdr->lpszValue)
1969 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
1971 LPSTR lpsztmp;
1972 INT len;
1974 len = strlen(value);
1976 if (len <= 0)
1978 /* if custom header delete from array */
1979 HeapFree(GetProcessHeap(), 0, lphttpHdr->lpszValue);
1980 lphttpHdr->lpszValue = NULL;
1981 bSuccess = TRUE;
1983 else
1985 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, len+1);
1986 if (lpsztmp)
1988 lphttpHdr->lpszValue = lpsztmp;
1989 strcpy(lpsztmp, value);
1990 bSuccess = TRUE;
1992 else
1994 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
1995 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1999 else if (dwModifier & COALESCEFLASG)
2001 LPSTR lpsztmp;
2002 CHAR ch = 0;
2003 INT len = 0;
2004 INT origlen = strlen(lphttpHdr->lpszValue);
2005 INT valuelen = strlen(value);
2007 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2009 ch = ',';
2010 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2012 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2014 ch = ';';
2015 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2018 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2020 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, len+1);
2021 if (lpsztmp)
2023 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2024 if (ch > 0)
2026 lphttpHdr->lpszValue[origlen] = ch;
2027 origlen++;
2030 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen);
2031 lphttpHdr->lpszValue[len] = '\0';
2032 bSuccess = TRUE;
2034 else
2036 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2037 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2041 TRACE("<-- %d\n",bSuccess);
2042 return bSuccess;
2046 /***********************************************************************
2047 * HTTP_CloseConnection (internal)
2049 * Close socket connection
2052 VOID HTTP_CloseConnection(LPWININETHTTPREQA lpwhr)
2056 LPWININETHTTPSESSIONA lpwhs = NULL;
2057 LPWININETAPPINFOA hIC = NULL;
2059 TRACE("%p\n",lpwhr);
2061 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
2062 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
2064 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
2065 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2067 if (NETCON_connected(&lpwhr->netConnection))
2069 NETCON_close(&lpwhr->netConnection);
2072 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
2073 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2077 /***********************************************************************
2078 * HTTP_CloseHTTPRequestHandle (internal)
2080 * Deallocate request handle
2083 void HTTP_CloseHTTPRequestHandle(LPWININETHTTPREQA lpwhr)
2085 int i;
2086 LPWININETHTTPSESSIONA lpwhs = NULL;
2087 LPWININETAPPINFOA hIC = NULL;
2089 TRACE("\n");
2091 if (NETCON_connected(&lpwhr->netConnection))
2092 HTTP_CloseConnection(lpwhr);
2094 lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
2095 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
2097 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
2098 INTERNET_STATUS_HANDLE_CLOSING, lpwhr,
2099 sizeof(HINTERNET));
2101 if (lpwhr->lpszPath)
2102 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2103 if (lpwhr->lpszVerb)
2104 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2105 if (lpwhr->lpszHostName)
2106 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
2108 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2110 if (lpwhr->StdHeaders[i].lpszField)
2111 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2112 if (lpwhr->StdHeaders[i].lpszValue)
2113 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2116 for (i = 0; i < lpwhr->nCustHeaders; i++)
2118 if (lpwhr->pCustHeaders[i].lpszField)
2119 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2120 if (lpwhr->pCustHeaders[i].lpszValue)
2121 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2124 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2125 HeapFree(GetProcessHeap(), 0, lpwhr);
2129 /***********************************************************************
2130 * HTTP_CloseHTTPSessionHandle (internal)
2132 * Deallocate session handle
2135 void HTTP_CloseHTTPSessionHandle(LPWININETHTTPSESSIONA lpwhs)
2137 LPWININETAPPINFOA hIC = NULL;
2138 TRACE("%p\n", lpwhs);
2140 hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
2142 SendAsyncCallback(hIC, lpwhs, lpwhs->hdr.dwContext,
2143 INTERNET_STATUS_HANDLE_CLOSING, lpwhs,
2144 sizeof(HINTERNET));
2146 if (lpwhs->lpszServerName)
2147 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2148 if (lpwhs->lpszUserName)
2149 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2150 HeapFree(GetProcessHeap(), 0, lpwhs);
2154 /***********************************************************************
2155 * HTTP_GetCustomHeaderIndex (internal)
2157 * Return index of custom header from header array
2160 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr, LPCSTR lpszField)
2162 INT index;
2164 TRACE("%s\n", lpszField);
2166 for (index = 0; index < lpwhr->nCustHeaders; index++)
2168 if (!strcasecmp(lpwhr->pCustHeaders[index].lpszField, lpszField))
2169 break;
2173 if (index >= lpwhr->nCustHeaders)
2174 index = -1;
2176 TRACE("Return: %d\n", index);
2177 return index;
2181 /***********************************************************************
2182 * HTTP_InsertCustomHeader (internal)
2184 * Insert header into array
2187 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr, LPHTTPHEADERA lpHdr)
2189 INT count;
2190 LPHTTPHEADERA lph = NULL;
2191 BOOL r = FALSE;
2193 TRACE("--> %s: %s\n", lpHdr->lpszField, lpHdr->lpszValue);
2194 count = lpwhr->nCustHeaders + 1;
2195 if (count > 1)
2196 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERA) * count);
2197 else
2198 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERA) * count);
2200 if (NULL != lph)
2202 lpwhr->pCustHeaders = lph;
2203 lpwhr->pCustHeaders[count-1].lpszField = HTTP_strdup(lpHdr->lpszField);
2204 lpwhr->pCustHeaders[count-1].lpszValue = HTTP_strdup(lpHdr->lpszValue);
2205 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2206 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2207 lpwhr->nCustHeaders++;
2208 r = TRUE;
2210 else
2212 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2215 return r;
2219 /***********************************************************************
2220 * HTTP_DeleteCustomHeader (internal)
2222 * Delete header from array
2223 * If this function is called, the indexs may change.
2225 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQA lpwhr, INT index)
2227 if( lpwhr->nCustHeaders <= 0 )
2228 return FALSE;
2229 if( lpwhr->nCustHeaders >= index )
2230 return FALSE;
2231 lpwhr->nCustHeaders--;
2233 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2234 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERA) );
2235 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERA) );
2237 return TRUE;
2240 /***********************************************************************
2241 * IsHostInProxyBypassList (@)
2243 * Undocumented
2246 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2248 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
2249 return FALSE;