Fixed some issues found by winapi_check.
[wine] / dlls / wininet / http.c
1 /* 
2  * Wininet - Http Implementation
3  *
4  * Copyright 1999 Corel Corporation
5  *
6  * Ulrich Czekalla
7  *
8  */
9
10 #include "config.h"
11
12 #include "windows.h"
13 #include "wininet.h"
14 #include "debugtools.h"
15 #include "winerror.h"
16 #include "heap.h"
17 #include "tchar.h"
18 #include "winsock.h"
19
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
23 #endif
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include "internet.h"
31
32 DEFAULT_DEBUG_CHANNEL(wininet)
33
34 #define HTTPHEADER " HTTP/1.0"
35 #define HTTPHOSTHEADER "\r\nHost: "
36 #define MAXHOSTNAME 100
37 #define MAX_FIELD_VALUE_LEN 256
38 #define MAX_FIELD_LEN 256
39
40
41 #define HTTP_REFERER    "Referer"
42 #define HTTP_ACCEPT             "Accept"
43
44 #define HTTP_ADDHDR_FLAG_ADD                            0x20000000
45 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW                     0x10000000
46 #define HTTP_ADDHDR_FLAG_COALESCE                       0x40000000
47 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA            0x40000000
48 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON        0x01000000
49 #define HTTP_ADDHDR_FLAG_REPLACE                        0x80000000
50 #define HTTP_ADDHDR_FLAG_REQ                            0x02000000
51
52
53 BOOL HTTP_OpenConnection(LPWININETHTTPREQA lpwhr);
54 int HTTP_WriteDataToStream(LPWININETHTTPREQA lpwhr, 
55         void *Buffer, int BytesToWrite);
56 int HTTP_ReadDataFromStream(LPWININETHTTPREQA lpwhr, 
57         void *Buffer, int BytesToRead);
58 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr);
59 BOOL HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr, LPCSTR field, LPCSTR value, DWORD dwModifier);
60 void HTTP_CloseConnection(LPWININETHTTPREQA lpwhr);
61 BOOL HTTP_InterpretHttpHeader(LPSTR buffer, LPSTR field, INT fieldlen, LPSTR value, INT valuelen);
62 INT HTTP_GetStdHeaderIndex(LPCSTR lpszField);
63 INT HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr, LPHTTPHEADERA lpHdr);
64 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr, LPCSTR lpszField);
65
66 /***********************************************************************
67  *           HttpAddRequestHeadersA (WININET.68)
68  *
69  * Adds one or more HTTP header to the request handler
70  *
71  * RETURNS
72  *    TRUE  on success
73  *    FALSE on failure
74  *
75  */
76 INTERNETAPI BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest, 
77         LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
78 {
79     LPSTR lpszStart;
80     LPSTR lpszEnd;
81     LPSTR buffer;
82     CHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
83     BOOL bSuccess = FALSE;
84     LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
85
86     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
87     {
88         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
89         return FALSE;
90     }
91
92     buffer = HEAP_strdupA(GetProcessHeap(), 0, lpszHeader);
93     lpszStart = buffer;
94
95     do
96     {
97         lpszEnd = lpszStart;
98
99         while (*lpszEnd != '\0')
100         {
101             if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
102                  break;
103             lpszEnd++;
104         }
105
106         if (*lpszEnd == '\0')
107             break;
108
109         *lpszEnd = '\0';
110
111         if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
112             bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
113
114         lpszStart = lpszEnd + 2; /* Jump over \0\n */
115
116     } while (bSuccess);
117
118     HeapFree(GetProcessHeap(), 0, buffer);
119     return bSuccess;
120 }
121
122
123 /***********************************************************************
124  *           HttpOpenRequestA (WININET.72)
125  *
126  * Open a HTTP request handle
127  *
128  * RETURNS
129  *    HINTERNET  a HTTP request handle on success
130  *    NULL       on failure
131  *
132  */
133 INTERNETAPI HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
134         LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
135         LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes, 
136         DWORD dwFlags, DWORD dwContext)
137 {
138     LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) hHttpSession;
139     LPWININETAPPINFOA hIC = NULL;
140
141     TRACE("\n");
142
143     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
144     {
145         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
146         return FALSE;
147     }
148
149     hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
150
151     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
152     {
153         WORKREQUEST workRequest;
154
155         workRequest.asyncall = HTTPOPENREQUESTA;
156         workRequest.HFTPSESSION = (DWORD)hHttpSession;
157         workRequest.LPSZVERB = (DWORD)HEAP_strdupA(GetProcessHeap(), 0, lpszVerb);
158         workRequest.LPSZOBJECTNAME = (DWORD)HEAP_strdupA(GetProcessHeap(), 0, lpszObjectName);
159         workRequest.LPSZVERSION = (DWORD)HEAP_strdupA(GetProcessHeap(), 0, lpszVersion);
160         workRequest.LPSZREFERRER = (DWORD)HEAP_strdupA(GetProcessHeap(), 0, lpszReferrer);
161         workRequest.LPSZACCEPTTYPES = (DWORD)lpszAcceptTypes;
162         workRequest.DWFLAGS = dwFlags;
163         workRequest.DWCONTEXT = dwContext;
164
165         return (HINTERNET)INTERNET_AsyncCall(&workRequest);
166     }
167     else
168     {
169         return HTTP_HttpOpenRequestA(hHttpSession, lpszVerb, lpszObjectName, 
170                 lpszVersion, lpszReferrer, lpszAcceptTypes, dwFlags, dwContext);
171     }
172 }
173
174
175 /***********************************************************************
176  *           HTTP_HttpOpenRequestA (internal)
177  *
178  * Open a HTTP request handle
179  *
180  * RETURNS
181  *    HINTERNET  a HTTP request handle on success
182  *    NULL       on failure
183  *
184  */
185 INTERNETAPI HINTERNET WINAPI HTTP_HttpOpenRequestA(HINTERNET hHttpSession,
186         LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
187         LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes, 
188         DWORD dwFlags, DWORD dwContext)
189 {
190     LPWININETHTTPSESSIONA lpwhs = (LPWININETHTTPSESSIONA) hHttpSession;
191     LPWININETAPPINFOA hIC = NULL;
192     LPWININETHTTPREQA lpwhr;
193
194     TRACE("\n");
195
196     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
197     {
198         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
199         return FALSE;
200     }
201
202     hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
203
204     lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQA));
205     if (NULL == lpwhr)
206     {
207         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
208         return (HINTERNET) NULL;
209     }
210
211     lpwhr->hdr.htype = WH_HHTTPREQ;
212     lpwhr->hdr.lpwhparent = hHttpSession;
213     lpwhr->hdr.dwFlags = dwFlags;
214     lpwhr->hdr.dwContext = dwContext;
215
216     if (NULL != lpszObjectName && strlen(lpszObjectName))
217         lpwhr->lpszPath = HEAP_strdupA(GetProcessHeap(), 0, lpszObjectName);
218
219     if (NULL != lpszReferrer && strlen(lpszReferrer))
220         HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
221
222     //! FIXME
223     if (NULL != lpszAcceptTypes && strlen(*lpszAcceptTypes))
224         HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, *lpszAcceptTypes, HTTP_ADDHDR_FLAG_COALESCE);
225
226     if (NULL == lpszVerb)
227         lpwhr->lpszVerb = HEAP_strdupA(GetProcessHeap(), 0, "GET");
228     else if (strlen(lpszVerb))
229         lpwhr->lpszVerb = HEAP_strdupA(GetProcessHeap(), 0, lpszVerb);
230
231     if (NULL != lpszReferrer)
232     {
233         char buf[MAXHOSTNAME];
234         URL_COMPONENTSA UrlComponents;
235
236         UrlComponents.lpszExtraInfo = NULL;
237         UrlComponents.lpszPassword = NULL;
238         UrlComponents.lpszScheme = NULL;
239         UrlComponents.lpszUrlPath = NULL;
240         UrlComponents.lpszUserName = NULL;
241         UrlComponents.lpszHostName = buf;
242         UrlComponents.dwHostNameLength = MAXHOSTNAME;
243
244         InternetCrackUrlA(lpszReferrer, 0, 0, &UrlComponents);
245         if (strlen(UrlComponents.lpszHostName))
246             lpwhr->lpszHostName = HEAP_strdupA(GetProcessHeap(), 0, UrlComponents.lpszHostName);
247     }
248
249     if (hIC->lpfnStatusCB)
250     {
251         INTERNET_ASYNC_RESULT iar;
252
253         iar.dwResult = (DWORD)lpwhr;
254         iar.dwError = ERROR_SUCCESS;
255
256         hIC->lpfnStatusCB(hHttpSession, dwContext, INTERNET_STATUS_HANDLE_CREATED,
257              &iar, sizeof(INTERNET_ASYNC_RESULT));
258     }
259
260     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC && hIC->lpfnStatusCB)
261     {
262         INTERNET_ASYNC_RESULT iar;
263                
264         iar.dwResult = (DWORD)lpwhr;
265         iar.dwError = lpwhr ? ERROR_SUCCESS : INTERNET_GetLastError();
266         hIC->lpfnStatusCB(hHttpSession, dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
267             &iar, sizeof(INTERNET_ASYNC_RESULT));
268     }
269
270     return (HINTERNET) lpwhr;
271 }
272
273
274 /***********************************************************************
275  *           HttpQueryInfoA (WININET.74)
276  *
277  * Queries for information about an HTTP request
278  *
279  * RETURNS
280  *    TRUE  on success
281  *    FALSE on failure
282  *
283  */
284 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
285         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
286 {
287     LPHTTPHEADERA lphttpHdr = NULL; 
288     BOOL bSuccess = FALSE;
289     LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
290                 
291     TRACE("(0x%08lx)--> %ld\n", dwInfoLevel, dwInfoLevel);
292
293     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
294     {
295         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
296         return FALSE;
297     }
298
299     /* Find requested header structure */
300     if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
301     {
302         INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPSTR)lpBuffer);
303
304         if (index < 0)
305             goto lend;
306
307         lphttpHdr = &lpwhr->pCustHeaders[index];
308     }
309     else
310     {
311         INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
312
313         if (index == HTTP_QUERY_RAW_HEADERS_CRLF || index == HTTP_QUERY_RAW_HEADERS)
314         {
315             INT i, delim, size = 0, cnt = 0;
316
317             delim = index == HTTP_QUERY_RAW_HEADERS_CRLF ? 2 : 1;
318
319            /* Calculate length of custom reuqest headers */
320            for (i = 0; i < lpwhr->nCustHeaders; i++)
321            {
322                if ((~lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST) && lpwhr->pCustHeaders[i].lpszField &&
323                    lpwhr->pCustHeaders[i].lpszValue)
324                {
325                   size += strlen(lpwhr->pCustHeaders[i].lpszField) + 
326                        strlen(lpwhr->pCustHeaders[i].lpszValue) + delim + 2;
327                }
328            }
329
330            /* Calculate the length of stadard request headers */
331            for (i = 0; i <= HTTP_QUERY_MAX; i++)
332            {
333               if ((~lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST) && lpwhr->StdHeaders[i].lpszField && 
334                    lpwhr->StdHeaders[i].lpszValue)
335               {
336                  size += strlen(lpwhr->StdHeaders[i].lpszField) + 
337                     strlen(lpwhr->StdHeaders[i].lpszValue) + delim + 2;
338               }
339            }
340
341            size += delim;
342
343            if (size + 1 > *lpdwBufferLength)
344            {
345               *lpdwBufferLength = size + 1;
346               INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
347               goto lend;
348            }
349
350            /* Append standard request heades */
351            for (i = 0; i <= HTTP_QUERY_MAX; i++)
352            {
353                if ((~lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST) &&
354                                            lpwhr->StdHeaders[i].lpszField &&
355                                            lpwhr->StdHeaders[i].lpszValue)
356                {
357                    cnt += sprintf(lpBuffer + cnt, "%s: %s%s", lpwhr->StdHeaders[i].lpszField, lpwhr->StdHeaders[i].lpszValue,
358                           index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "\0");
359                }
360             }
361
362             /* Append custom request heades */
363             for (i = 0; i < lpwhr->nCustHeaders; i++)
364             {
365                 if ((~lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST) &&
366                                                 lpwhr->pCustHeaders[i].lpszField &&
367                                                 lpwhr->pCustHeaders[i].lpszValue)
368                 {
369                    cnt += sprintf(lpBuffer + cnt, "%s: %s%s", 
370                     lpwhr->pCustHeaders[i].lpszField, lpwhr->pCustHeaders[i].lpszValue,
371                                         index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "\0");
372                 }
373             }
374
375             strcpy(lpBuffer + cnt, index == HTTP_QUERY_RAW_HEADERS_CRLF ? "\r\n" : "");
376
377            *lpdwBufferLength = cnt + delim;
378            bSuccess = TRUE;
379                 goto lend;
380         }
381         else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
382         { 
383             lphttpHdr = &lpwhr->StdHeaders[index];
384         }
385         else
386             goto lend;
387     }
388
389     /* Ensure header satisifies requested attributes */
390     if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) && 
391             (~lphttpHdr->wFlags & HDR_ISREQUEST))
392         goto lend;
393      
394     /* coalesce value to reuqested type */
395     if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
396     {
397        *(int *)lpBuffer = atoi(lphttpHdr->lpszValue);
398            bSuccess = TRUE;
399     }
400     else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
401     {
402         time_t tmpTime;
403         struct tm tmpTM;
404         SYSTEMTIME *STHook;
405              
406         tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
407
408         tmpTM = *gmtime(&tmpTime);
409         STHook = (SYSTEMTIME *) lpBuffer;
410         if(STHook==NULL)
411             goto lend;
412
413             STHook->wDay = tmpTM.tm_mday;
414             STHook->wHour = tmpTM.tm_hour;
415             STHook->wMilliseconds = 0;
416             STHook->wMinute = tmpTM.tm_min;
417             STHook->wDayOfWeek = tmpTM.tm_wday;
418             STHook->wMonth = tmpTM.tm_mon + 1;
419             STHook->wSecond = tmpTM.tm_sec;
420             STHook->wYear = tmpTM.tm_year;
421
422             bSuccess = TRUE;
423     }
424     else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
425     {
426             if (*lpdwIndex >= lphttpHdr->wCount)
427                 {
428                 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
429                 }
430             else
431             {
432             //! Copy strncpy(lpBuffer, lphttpHdr[*lpdwIndex], len);
433             (*lpdwIndex)++;
434             }
435     }
436     else
437     {
438         INT len = strlen(lphttpHdr->lpszValue);
439
440         if (len + 1 > *lpdwBufferLength)
441         {
442             *lpdwBufferLength = len + 1;
443             INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
444             goto lend;
445         }
446
447         strncpy(lpBuffer, lphttpHdr->lpszValue, len);
448         *lpdwBufferLength = len;
449         bSuccess = TRUE; 
450     }
451
452 lend:
453     TRACE("%d <--\n", bSuccess);
454     return bSuccess;
455 }
456
457 /***********************************************************************
458  *           HttpSendRequestA (WININET.76)
459  *
460  * Sends the specified request to the HTTP server
461  *
462  * RETURNS
463  *    TRUE  on success
464  *    FALSE on failure
465  *
466  */
467 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
468         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
469 {       
470     LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
471     LPWININETHTTPSESSIONA lpwhs = NULL;
472     LPWININETAPPINFOA hIC = NULL;
473
474     TRACE("0x%08lx\n", (unsigned long)hHttpRequest);
475
476     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
477     {
478         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
479         return FALSE;
480     }
481
482     lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
483     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
484     {
485         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
486         return FALSE;
487     }
488
489     hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
490     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT)
491     {
492         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
493         return FALSE;
494     }
495
496     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
497     {
498         WORKREQUEST workRequest;
499
500         workRequest.asyncall = HTTPSENDREQUESTA;
501         workRequest.HFTPSESSION = (DWORD)hHttpRequest;
502         workRequest.LPSZHEADER = (DWORD)HEAP_strdupA(GetProcessHeap(), 0, lpszHeaders);
503         workRequest.DWHEADERLENGTH = dwHeaderLength;
504         workRequest.LPOPTIONAL = (DWORD)lpOptional;
505         workRequest.DWOPTIONALLENGTH = dwOptionalLength;
506
507         return INTERNET_AsyncCall(&workRequest);
508     }
509     else
510     {
511         return HTTP_HttpSendRequestA(hHttpRequest, lpszHeaders, 
512                 dwHeaderLength, lpOptional, dwOptionalLength);
513     }
514 }
515
516
517 /***********************************************************************
518  *           HTTP_HttpSendRequestA (internal)
519  *
520  * Sends the specified request to the HTTP server
521  *
522  * RETURNS
523  *    TRUE  on success
524  *    FALSE on failure
525  *
526  */
527 BOOL WINAPI HTTP_HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
528         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
529 {
530     INT cnt;
531     INT i;
532     BOOL bSuccess = FALSE;
533     LPSTR requestString = NULL;
534     INT requestStringLen;
535     INT headerLength = 0;
536     LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hHttpRequest;
537     LPWININETHTTPSESSIONA lpwhs = NULL;
538     LPWININETAPPINFOA hIC = NULL;
539
540     TRACE("0x%08lx\n", (ULONG)hHttpRequest);
541
542     /* Verify our tree of internet handles */
543     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
544     {
545         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
546         return FALSE;
547     }
548
549     lpwhs = (LPWININETHTTPSESSIONA) lpwhr->hdr.lpwhparent;
550     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
551     {
552         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
553         return FALSE;
554     }
555
556     hIC = (LPWININETAPPINFOA) lpwhs->hdr.lpwhparent;
557     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT)
558     {
559         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
560         return FALSE;
561     }
562
563     /* Clear any error information */
564     INTERNET_SetLastError(0);
565
566     /* We must have a verb */
567     if (NULL == lpwhr->lpszVerb)
568     {
569             goto lend;
570     }
571
572     /* If we don't have a path we set it to root */
573     if (NULL == lpwhr->lpszPath)
574         lpwhr->lpszPath = HEAP_strdupA(GetProcessHeap(), 0, "/");
575
576     /* Calculate length of request string */
577     requestStringLen = 
578         strlen(lpwhr->lpszVerb) +
579         strlen(lpwhr->lpszPath) +
580         (lpwhr->lpszHostName ? (strlen(HTTPHOSTHEADER) + strlen(lpwhr->lpszHostName)) : 0) +
581         strlen(HTTPHEADER) +
582         5; /* " \r\n\r\n" */
583
584     /* Add length of passed headers */
585     if (lpszHeaders)
586     {
587         headerLength = -1 == dwHeaderLength ?  strlen(lpszHeaders) : dwHeaderLength; 
588         requestStringLen += headerLength +  2; /* \r\n */
589     }
590
591     /* Calculate length of custom reuqest headers */
592     for (i = 0; i < lpwhr->nCustHeaders; i++)
593     {
594             if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
595             {
596             requestStringLen += strlen(lpwhr->pCustHeaders[i].lpszField) + 
597                 strlen(lpwhr->pCustHeaders[i].lpszValue) + 4; /*: \r\n */
598             }
599     }
600
601     /* Calculate the length of stadard request headers */
602     for (i = 0; i <= HTTP_QUERY_MAX; i++)
603     {
604        if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
605        {
606           requestStringLen += strlen(lpwhr->StdHeaders[i].lpszField) + 
607              strlen(lpwhr->StdHeaders[i].lpszValue) + 4; /*: \r\n */
608        }
609     }
610
611     /* Allocate string to hold entire request */
612     requestString = HeapAlloc(GetProcessHeap(), 0, requestStringLen + 1);
613     if (NULL == requestString)
614     {
615         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
616         goto lend;
617     }
618
619     /* Build request string */
620     cnt = sprintf(requestString, "%s %s%s%s",
621         lpwhr->lpszVerb,
622         lpwhr->lpszPath,
623         lpwhr->lpszHostName ? (HTTPHEADER HTTPHOSTHEADER) : HTTPHEADER,
624         lpwhr->lpszHostName ? lpwhr->lpszHostName : "");
625
626     /* Append standard request heades */
627     for (i = 0; i <= HTTP_QUERY_MAX; i++)
628     {
629        if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
630        {
631            cnt += sprintf(requestString + cnt, "\r\n%s: %s", 
632                lpwhr->StdHeaders[i].lpszField, lpwhr->StdHeaders[i].lpszValue);
633        }
634     }
635
636     /* Append custom request heades */
637     for (i = 0; i < lpwhr->nCustHeaders; i++)
638     {
639        if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
640        {
641            cnt += sprintf(requestString + cnt, "\r\n%s: %s", 
642                lpwhr->pCustHeaders[i].lpszField, lpwhr->pCustHeaders[i].lpszValue);
643        }
644     }
645
646     /* Append passed request headers */
647     if (lpszHeaders)
648     {
649         strcpy(requestString + cnt, "\r\n");
650         cnt += 2;
651         strcpy(requestString + cnt, lpszHeaders);
652         cnt += headerLength;
653     }
654
655     /* Set termination string for request */
656     strcpy(requestString + cnt, "\r\n\r\n");
657
658     if (hIC->lpfnStatusCB)
659         hIC->lpfnStatusCB(hHttpRequest, lpwhr->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
660
661     TRACE("(%s) len(%d)\n", requestString, requestStringLen);
662     /* Send the request and store the results */
663     if (!HTTP_OpenConnection(lpwhr))
664         goto lend;
665
666     cnt = INTERNET_WriteDataToStream(lpwhr->nSocketFD, requestString, requestStringLen);
667
668     if (cnt < 0)
669         goto lend;
670
671     if (HTTP_GetResponseHeaders(lpwhr))
672             bSuccess = TRUE;
673
674 lend:
675
676     if (requestString)
677         HeapFree(GetProcessHeap(), 0, requestString);
678
679     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC  && hIC->lpfnStatusCB)
680     {
681         INTERNET_ASYNC_RESULT iar;
682                
683         iar.dwResult = (DWORD)bSuccess;
684         iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
685         hIC->lpfnStatusCB(hHttpRequest, lpwhr->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
686             &iar, sizeof(INTERNET_ASYNC_RESULT));
687     }
688
689     TRACE("<--\n");
690     return bSuccess;
691 }
692
693
694 /***********************************************************************
695  *           HTTP_Connect  (internal)
696  *
697  * Create http session handle
698  *
699  * RETURNS
700  *   HINTERNET a session handle on success
701  *   NULL on failure
702  *
703  */
704 HINTERNET HTTP_Connect(HINTERNET hInternet, LPCSTR lpszServerName, 
705         INTERNET_PORT nServerPort, LPCSTR lpszUserName,
706         LPCSTR lpszPassword, DWORD dwFlags, DWORD dwContext)
707 {
708     BOOL bSuccess = FALSE;
709     LPWININETAPPINFOA hIC = NULL;
710     LPWININETHTTPSESSIONA lpwhs = NULL;
711
712     TRACE("\n");
713
714     if (((LPWININETHANDLEHEADER)hInternet)->htype != WH_HINIT)
715         goto lerror;
716
717     hIC = (LPWININETAPPINFOA) hInternet;
718
719     lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONA));
720     if (NULL == lpwhs)
721     {
722         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
723         goto lerror;
724     }
725
726     if (hIC->lpfnStatusCB)
727         hIC->lpfnStatusCB(hInternet, dwContext, INTERNET_STATUS_RESOLVING_NAME,
728            (LPVOID)lpszServerName, strlen(lpszServerName));
729
730     if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
731         nServerPort = INTERNET_DEFAULT_HTTP_PORT;
732
733     if (!GetAddress(lpszServerName, nServerPort, &lpwhs->phostent, &lpwhs->socketAddress))
734     {
735             INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
736             goto lerror;
737     }
738
739     if (hIC->lpfnStatusCB)
740         hIC->lpfnStatusCB(hInternet, dwContext, INTERNET_STATUS_NAME_RESOLVED,
741            (LPVOID)lpszServerName, strlen(lpszServerName));
742
743     lpwhs->hdr.htype = WH_HHTTPSESSION;
744     lpwhs->hdr.lpwhparent = (LPWININETHANDLEHEADER)hInternet;
745     lpwhs->hdr.dwFlags = dwFlags;
746     lpwhs->hdr.dwContext = dwContext;
747     if (NULL != lpszServerName)
748         lpwhs->lpszServerName = HEAP_strdupA(GetProcessHeap(), 0, lpszServerName);
749     if (NULL != lpszUserName)
750         lpwhs->lpszUserName = HEAP_strdupA(GetProcessHeap(), 0, lpszUserName);
751     lpwhs->nServerPort = nServerPort;
752
753     if (hIC->lpfnStatusCB)
754     {
755         INTERNET_ASYNC_RESULT iar;
756
757         iar.dwResult = (DWORD)lpwhs;
758         iar.dwError = ERROR_SUCCESS;
759
760         hIC->lpfnStatusCB(hInternet, dwContext, INTERNET_STATUS_HANDLE_CREATED,
761              &iar, sizeof(INTERNET_ASYNC_RESULT));
762     }
763
764     bSuccess = TRUE;
765
766 lerror:
767     if (!bSuccess && lpwhs)
768     {
769         HeapFree(GetProcessHeap(), 0, lpwhs);
770         lpwhs = NULL;
771     }
772
773     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC && hIC->lpfnStatusCB)
774     {
775         INTERNET_ASYNC_RESULT iar;
776                
777         iar.dwResult = (DWORD)lpwhs;
778         iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
779         hIC->lpfnStatusCB(hInternet, dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
780             &iar, sizeof(INTERNET_ASYNC_RESULT));
781     }
782 TRACE("<--\n");
783     return (HINTERNET)lpwhs;
784 }
785
786
787 /***********************************************************************
788  *           HTTP_OpenConnection (internal)
789  *
790  * Connect to a web server
791  *
792  * RETURNS
793  *
794  *   TRUE  on success
795  *   FALSE on failure
796  */
797 BOOL HTTP_OpenConnection(LPWININETHTTPREQA lpwhr)
798 {
799     BOOL bSuccess = FALSE;
800     INT result;
801     LPWININETHTTPSESSIONA lpwhs;
802
803     TRACE("\n");
804
805     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
806     {
807         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
808         goto lend;
809     }
810
811     lpwhs = (LPWININETHTTPSESSIONA)lpwhr->hdr.lpwhparent;
812
813     lpwhr->nSocketFD = socket(lpwhs->phostent->h_addrtype,SOCK_STREAM,0);
814     if (INVALID_SOCKET == lpwhr->nSocketFD)
815     {
816         WARN("Socket creation failed\n");
817         goto lend;
818     }
819
820     result = connect(lpwhr->nSocketFD, (struct sockaddr *)&lpwhs->socketAddress,
821         sizeof(lpwhs->socketAddress));
822
823     if (SOCKET_ERROR == result)
824     {
825        WARN("Unable to connect to host: %d\n", errno);
826        goto lend;
827     }
828
829     bSuccess = TRUE;
830
831 lend:
832     TRACE(": %d\n", bSuccess);
833     return bSuccess;
834 }
835
836
837 /***********************************************************************
838  *           HTTP_GetResponseHeaders (internal)
839  *
840  * Read server response
841  *
842  * RETURNS
843  *
844  *   TRUE  on success
845  *   FALSE on error
846  */
847 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQA lpwhr)
848 {
849     INT cbreaks = 0;
850     CHAR buffer[MAX_REPLY_LEN];
851     DWORD buflen = MAX_REPLY_LEN;
852     BOOL bSuccess = FALSE;
853     CHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
854
855     TRACE("\n");
856
857     if (INVALID_SOCKET == lpwhr->nSocketFD)
858         goto lend;
859
860     /* 
861      * We should first receive 'HTTP/1.x nnn' where nnn is the status code.
862      */
863     if (!INTERNET_GetNextLine(lpwhr->nSocketFD, buffer, &buflen))
864         goto lend;
865
866     if (strncmp(buffer, "HTTP", 4) != 0)
867         goto lend;
868         
869     buffer[12]='\0';
870     HTTP_ProcessHeader(lpwhr, "Status", buffer+9, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
871
872     /* Parse each response line */
873     do
874     {
875         buflen = MAX_REPLY_LEN;
876         if (INTERNET_GetNextLine(lpwhr->nSocketFD, buffer, &buflen))
877         {
878             if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
879                 break;
880
881             HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
882         }
883         else
884         {
885             cbreaks++;
886             if (cbreaks >= 2)
887                break;
888         }
889     }while(1);
890
891     bSuccess = TRUE;
892
893 lend:
894
895     return bSuccess;
896 }
897
898
899 /***********************************************************************
900  *           HTTP_InterpretHttpHeader (internal)
901  *
902  * Parse server response
903  *
904  * RETURNS
905  *
906  *   TRUE  on success
907  *   FALSE on error
908  */
909 INT stripSpaces(LPCSTR lpszSrc, LPSTR lpszStart, INT *len)
910 {
911     LPCSTR lpsztmp;
912     INT srclen;
913
914     srclen = 0;
915
916     while (*lpszSrc == ' ' && *lpszSrc != '\0')
917         lpszSrc++;
918         
919     lpsztmp = lpszSrc;
920     while(*lpsztmp != '\0')
921     {
922         if (*lpsztmp != ' ')
923             srclen = lpsztmp - lpszSrc + 1;
924
925         lpsztmp++;
926     }
927
928     *len = min(*len, srclen);
929     strncpy(lpszStart, lpszSrc, *len);
930     lpszStart[*len] = '\0';
931
932     return *len;
933 }
934
935
936 BOOL HTTP_InterpretHttpHeader(LPSTR buffer, LPSTR field, INT fieldlen, LPSTR value, INT valuelen)
937 {
938     CHAR *pd;
939     BOOL bSuccess = FALSE;
940
941     TRACE("\n");
942
943     *field = '\0';
944     *value = '\0';
945
946     pd = strchr(buffer, ':');
947     if (pd)
948     {
949         *pd = '\0';
950         if (stripSpaces(buffer, field, &fieldlen) > 0)
951         {
952             if (stripSpaces(pd+1, value, &valuelen) > 0)
953                 bSuccess = TRUE;
954         }
955     }
956
957     TRACE("%d: field(%s) Value(%s)\n", bSuccess, field, value);
958     return bSuccess;
959 }
960
961
962 /***********************************************************************
963  *           HTTP_GetStdHeaderIndex (internal)
964  *
965  * Lookup field index in stadard http header array
966  *
967  * FIXME: This should be stuffed into a hash table
968  */
969 INT HTTP_GetStdHeaderIndex(LPCSTR lpszField)
970 {
971     INT index = -1;
972
973     if (!_stricmp(lpszField, "Content-Length"))
974         index = HTTP_QUERY_CONTENT_LENGTH;
975     else if (!_stricmp(lpszField,"Status"))
976         index = HTTP_QUERY_STATUS_CODE;
977     else if (!_stricmp(lpszField,"Content-Type"))
978         index = HTTP_QUERY_CONTENT_TYPE;
979     else if (!_stricmp(lpszField,"Last-Modified"))
980         index = HTTP_QUERY_LAST_MODIFIED;
981     else if (!_stricmp(lpszField,"Location"))
982         index = HTTP_QUERY_LOCATION;
983     else if (!_stricmp(lpszField,"Accept"))
984         index = HTTP_QUERY_ACCEPT;
985     else if (!_stricmp(lpszField,"Referer"))
986         index = HTTP_QUERY_REFERER;
987     else if (!_stricmp(lpszField,"Content-Transfer-Encoding"))
988         index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
989     else
990     {
991        FIXME("Couldn't find %s in standard header table\n", lpszField);
992     }
993
994     return index;
995 }
996
997
998 /***********************************************************************
999  *           HTTP_ProcessHeader (internal)
1000  *
1001  * Stuff header into header tables according to <dwModifier>
1002  *
1003  */
1004
1005 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1006
1007 BOOL HTTP_ProcessHeader(LPWININETHTTPREQA lpwhr, LPCSTR field, LPCSTR value, DWORD dwModifier)
1008 {
1009     LPHTTPHEADERA lphttpHdr = NULL;
1010     BOOL bSuccess = FALSE;
1011     INT index;
1012
1013     TRACE("%s:%s - 0x%08x\n", field, value, (unsigned int)dwModifier);
1014
1015     /* Adjust modifier flags */
1016     if (dwModifier & COALESCEFLASG)
1017         dwModifier |= HTTP_ADDHDR_FLAG_ADD;
1018
1019     /* Try to get index into standard header array */
1020     index = HTTP_GetStdHeaderIndex(field);
1021     if (index >= 0)
1022     {
1023         lphttpHdr = &lpwhr->StdHeaders[index];
1024     }
1025     else /* Find or create new custom header */
1026     {
1027         index = HTTP_GetCustomHeaderIndex(lpwhr, field);
1028         if (index >= 0)
1029         {
1030             if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
1031             {
1032                 return FALSE;
1033             }
1034             lphttpHdr = &lpwhr->pCustHeaders[index];
1035         }
1036         else
1037         {
1038             HTTPHEADERA hdr;
1039
1040             hdr.lpszField = (LPSTR)field;
1041             hdr.lpszValue = (LPSTR)value;
1042             hdr.wFlags = hdr.wCount = 0;
1043
1044             if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1045                 hdr.wFlags |= HDR_ISREQUEST;
1046
1047             index = HTTP_InsertCustomHeader(lpwhr, &hdr);
1048             return index >= 0;
1049         }
1050     }
1051  
1052     if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1053         lphttpHdr->wFlags |= HDR_ISREQUEST;
1054     else
1055         lphttpHdr->wFlags &= ~HDR_ISREQUEST;
1056   
1057     if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
1058     {
1059         INT slen;
1060
1061         if (!lpwhr->StdHeaders[index].lpszField)
1062         {
1063             lphttpHdr->lpszField = HEAP_strdupA(GetProcessHeap(), 0, field);
1064
1065             if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
1066                 lphttpHdr->wFlags |= HDR_ISREQUEST;
1067         }
1068
1069         slen = strlen(value) + 1;
1070         lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen);
1071         if (lphttpHdr->lpszValue)
1072         {
1073             memcpy(lphttpHdr->lpszValue, value, slen);
1074             bSuccess = TRUE;
1075         }
1076         else
1077         {
1078             INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1079         }
1080     }
1081     else if (lphttpHdr->lpszValue) 
1082     {
1083         if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE) 
1084         {
1085             LPSTR lpsztmp;
1086             INT len;
1087
1088             len = strlen(value);
1089
1090             if (len <= 0)
1091             {
1092                 //! if custom header delete from array
1093                 HeapFree(GetProcessHeap(), 0, lphttpHdr->lpszValue);
1094                 lphttpHdr->lpszValue = NULL;
1095                 bSuccess = TRUE;
1096             }
1097             else
1098             {
1099                 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,  lphttpHdr->lpszValue, len+1);
1100                 if (lpsztmp)
1101                 {
1102                     lphttpHdr->lpszValue = lpsztmp;
1103                     strcpy(lpsztmp, value);
1104                     bSuccess = TRUE;
1105                 }
1106                 else
1107                 {
1108                     INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1109                 }
1110             }
1111         }
1112         else if (dwModifier & COALESCEFLASG)
1113         {
1114             LPSTR lpsztmp;
1115             CHAR ch = 0;
1116             INT len = 0;
1117             INT origlen = strlen(lphttpHdr->lpszValue);
1118             INT valuelen = strlen(value);
1119
1120             if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
1121             {
1122                 ch = ',';
1123                 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
1124             }
1125             else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
1126             {
1127                 ch = ';';
1128                 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
1129             }
1130
1131             len = origlen + valuelen + (ch > 0) ? 1 : 0;
1132
1133             lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,  lphttpHdr->lpszValue, len+1);
1134             if (lpsztmp)
1135             {
1136                 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */ 
1137                 if (ch > 0)
1138                 {
1139                     lphttpHdr->lpszValue[origlen] = ch;
1140                     origlen++;
1141                 }
1142
1143                 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen);
1144                 lphttpHdr->lpszValue[len] = '\0';
1145                 bSuccess = TRUE;
1146             }
1147             else
1148             {
1149                 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1150             }
1151         }
1152     }
1153
1154     return bSuccess;
1155 }
1156
1157
1158 /***********************************************************************
1159  *           HTTP_CloseConnection (internal)
1160  *
1161  * Close socket connection
1162  *
1163  */
1164 VOID HTTP_CloseConnection(LPWININETHTTPREQA lpwhr)
1165 {
1166         if (lpwhr->nSocketFD != INVALID_SOCKET)
1167         {
1168                 close(lpwhr->nSocketFD);
1169                 lpwhr->nSocketFD = INVALID_SOCKET;
1170         }
1171 }
1172
1173
1174 /***********************************************************************
1175  *           HTTP_CloseHTTPRequestHandle (internal)
1176  *
1177  * Deallocate request handle
1178  *
1179  */
1180 void HTTP_CloseHTTPRequestHandle(LPWININETHTTPREQA lpwhr)
1181 {
1182     int i;
1183
1184     TRACE("\n");
1185
1186     if (lpwhr->nSocketFD != INVALID_SOCKET)
1187         HTTP_CloseConnection(lpwhr);
1188
1189     if (lpwhr->lpszPath)
1190         HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1191     if (lpwhr->lpszVerb)
1192         HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1193     if (lpwhr->lpszHostName)
1194         HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1195
1196     for (i = 0; i <= HTTP_QUERY_MAX; i++)
1197     {
1198            if (lpwhr->StdHeaders[i].lpszField)
1199             HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
1200            if (lpwhr->StdHeaders[i].lpszValue)
1201             HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
1202     }
1203
1204     for (i = 0; i < lpwhr->nCustHeaders; i++)
1205     {
1206            if (lpwhr->pCustHeaders[i].lpszField)
1207             HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1208            if (lpwhr->pCustHeaders[i].lpszValue)
1209             HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1210     }
1211
1212     HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1213     HeapFree(GetProcessHeap(), 0, lpwhr);
1214 }
1215
1216
1217 /***********************************************************************
1218  *           HTTP_CloseHTTPSessionHandle (internal)
1219  *
1220  * Deallocate session handle
1221  *
1222  */
1223 void HTTP_CloseHTTPSessionHandle(LPWININETHTTPSESSIONA lpwhs)
1224 {
1225     TRACE("\n");
1226
1227     if (lpwhs->lpszServerName)
1228         HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1229     if (lpwhs->lpszUserName)
1230         HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1231     HeapFree(GetProcessHeap(), 0, lpwhs);
1232 }
1233
1234
1235 /***********************************************************************
1236  *           HTTP_GetCustomHeaderIndex (internal)
1237  *
1238  * Return index of custom header from header array
1239  *
1240  */
1241 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQA lpwhr, LPCSTR lpszField)
1242 {
1243     INT index;
1244
1245     TRACE("%s\n", lpszField);
1246
1247     for (index = 0; index < lpwhr->nCustHeaders; index++)
1248     {
1249         if (!_stricmp(lpwhr->pCustHeaders[index].lpszField, lpszField))
1250             break;
1251
1252     }
1253
1254     if (index >= lpwhr->nCustHeaders)
1255         index = -1;
1256
1257     TRACE("Return: %d\n", index);
1258     return index;
1259 }
1260
1261
1262 /***********************************************************************
1263  *           HTTP_InsertCustomHeader (internal)
1264  *
1265  * Insert header into array
1266  *
1267  */
1268 INT HTTP_InsertCustomHeader(LPWININETHTTPREQA lpwhr, LPHTTPHEADERA lpHdr)
1269 {
1270     INT count;
1271     LPHTTPHEADERA lph = NULL;
1272
1273     TRACE("%s: %s\n", lpHdr->lpszField, lpHdr->lpszValue);
1274     count = lpwhr->nCustHeaders + 1;
1275     if (count > 1)
1276         lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERA) * count);
1277     else
1278         lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERA) * count);
1279
1280     if (NULL != lph)
1281     {
1282         lpwhr->pCustHeaders = lph;
1283         lpwhr->pCustHeaders[count-1].lpszField = HEAP_strdupA(GetProcessHeap(), 0, lpHdr->lpszField);
1284         lpwhr->pCustHeaders[count-1].lpszValue = HEAP_strdupA(GetProcessHeap(), 0, lpHdr->lpszValue);
1285         lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
1286         lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
1287         lpwhr->nCustHeaders++;
1288     }
1289     else
1290     {
1291         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1292         count = 0;
1293     }
1294
1295     TRACE("%d <--\n", count-1);
1296     return count - 1;
1297 }
1298
1299
1300 /***********************************************************************
1301  *           HTTP_DeleteCustomHeader (internal)
1302  *
1303  * Delete header from array
1304  *
1305  */
1306 BOOL HTTP_DeleteCustomHeader(INT index)
1307 {
1308     TRACE("\n");
1309     return FALSE;
1310 }