summaryrefslogtreecommitdiff
path: root/common/src/nx_http_proxy_client.c
blob: 4c749551d4daf686849a5a9ad2a95aaf17b29c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */ 
/** NetX Component                                                        */
/**                                                                       */
/**   HTTP Proxy Protocol                                                 */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/
#define NX_SOURCE_CODE


#include "nx_ip.h"
#include "nx_ipv6.h"
#include "nx_packet.h"
#include "nx_tcp.h"
#include "nx_http_proxy_client.h"

#ifdef NX_ENABLE_HTTP_PROXY
#define NX_HTTP_CRLF       "\r\n"
#define NX_HTTP_CRLF_SIZE  2

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_http_proxy_client_enable                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Wenhui Xie, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the HTTP Proxy enable call.      */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                Pointer to IP instance        */
/*    proxy_server_ip                       IP address of proxy server    */
/*    proxy_server_port                     Port of proxy server          */
/*    username                              Pointer to username           */
/*    username_length                       Length of username            */
/*    password                              Pointer to password           */
/*    password_length                       Length of password            */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_http_proxy_client_enable          Actual HTTP Proxy enable call */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nxe_http_proxy_client_enable(NX_IP *ip_ptr, NXD_ADDRESS *proxy_server_ip, UINT proxy_server_port,
                                   UCHAR *username, UINT username_length, UCHAR *password, UINT password_length)
{
UINT        status;

    /* Check for invalid input pointers.  */
    if ((ip_ptr == NX_NULL) || (ip_ptr -> nx_ip_id != NX_IP_ID) || 
        (proxy_server_ip == NX_NULL) || (proxy_server_port == 0) ||
        ((username == NX_NULL) && (username_length != 0)) ||
        ((password == NX_NULL) && (password_length != 0)))
    {
        return(NX_PTR_ERROR);
    }

    /* Check the length of username and password.  */
    if ((username_length > NX_HTTP_PROXY_MAX_USERNAME) ||
        (password_length > NX_HTTP_PROXY_MAX_PASSWORD))
    {
        return(NX_SIZE_ERROR);
    }

    /* Check that the server IP address version is either IPv4 or IPv6. */
    if ((proxy_server_ip -> nxd_ip_version != NX_IP_VERSION_V4) &&
        (proxy_server_ip -> nxd_ip_version != NX_IP_VERSION_V6))
    {

        return(NX_IP_ADDRESS_ERROR);
    }

#ifndef NX_DISABLE_IPV4
    /* Check for a valid server IP address if the server_ip is version IPv4.  */
    if (proxy_server_ip -> nxd_ip_version == NX_IP_VERSION_V4)
    {
        if (((proxy_server_ip -> nxd_ip_address.v4 & NX_IP_CLASS_A_MASK) != NX_IP_CLASS_A_TYPE) &&
            ((proxy_server_ip -> nxd_ip_address.v4 & NX_IP_CLASS_B_MASK) != NX_IP_CLASS_B_TYPE) &&
            ((proxy_server_ip -> nxd_ip_address.v4 & NX_IP_CLASS_C_MASK) != NX_IP_CLASS_C_TYPE))
        {
            return(NX_IP_ADDRESS_ERROR);
        }
    }
#endif /* !NX_DISABLE_IPV4  */

    /* Check for an invalid port.  */
    if (((ULONG)proxy_server_port) > (ULONG)NX_MAX_PORT)
    {
        return(NX_INVALID_PORT);
    }

    /* Call actual HTTP proxy enable function.  */
    status = _nx_http_proxy_client_enable(ip_ptr, proxy_server_ip, proxy_server_port, username,
                                          username_length, password, password_length);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_http_proxy_client_enable                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Wenhui Xie, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function enables the HTTP Proxy and sets the information of    */
/*    the HTTP Proxy server.                                              */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                Pointer to IP instance        */
/*    proxy_server_ip                       IP address of proxy server    */
/*    proxy_server_port                     Port of proxy server          */
/*    username                              Pointer to username           */
/*    username_length                       Length of username            */
/*    password                              Pointer to password           */
/*    password_length                       Length of password            */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    COPY_IPV6_ADDRESS                     Copy IPv6 address             */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nx_http_proxy_client_enable(NX_IP *ip_ptr, NXD_ADDRESS *proxy_server_ip, UINT proxy_server_port,
                                  UCHAR *username, UINT username_length, UCHAR *password, UINT password_length)
{

UINT status;
UCHAR string[NX_HTTP_PROXY_MAX_USERNAME + NX_HTTP_PROXY_MAX_PASSWORD + 2];

    /* Set the IP address of HTTP proxy.  */
    ip_ptr -> nx_ip_http_proxy_ip_address.nxd_ip_version = proxy_server_ip -> nxd_ip_version;

#ifndef NX_DISABLE_IPV4
    if (proxy_server_ip -> nxd_ip_version == NX_IP_VERSION_V4)
    {

        /* Copy the IPv4 address */
        ip_ptr -> nx_ip_http_proxy_ip_address.nxd_ip_address.v4 = proxy_server_ip -> nxd_ip_address.v4;
    }
#endif /* !NX_DISABLE_IPV4  */

#ifdef FEATURE_NX_IPV6
    if (proxy_server_ip -> nxd_ip_version == NX_IP_VERSION_V6)
    {

        /* Copy the IPv6 address */
        COPY_IPV6_ADDRESS(proxy_server_ip -> nxd_ip_address.v6,
                          ip_ptr -> nx_ip_http_proxy_ip_address.nxd_ip_address.v6);
    }
#endif /* FEATURE_NX_IPV6 */
    
    /* Set the port of HTTP proxy.  */
    ip_ptr -> nx_ip_http_proxy_port = (USHORT)proxy_server_port;

    /* Determine if basic authentication is required.  */
    if ((username_length) && (password_length))
    {

        /* Encode the "name:password" into authentication buffer.  */

        /* Place the name and password in a single string.  */

        /* Copy the name into the merged string.  */
        memcpy(string, username, username_length); /* Use case of memcpy is verified. */

        /* Insert the colon.  */
        string[username_length] =  ':';

        /* Copy the password into the merged string.  */
        memcpy(&string[username_length + 1], password, password_length); /* Use case of memcpy is verified. */

        /* Make combined string NULL terminated.  */
        string[username_length + password_length + 1] =  NX_NULL;

        /* Now encode the username:password string.  */
        status = _nx_utility_base64_encode((UCHAR *)string, username_length + password_length + 1,
                                           (UCHAR *)ip_ptr -> nx_ip_http_proxy_authentication,
                                           sizeof(ip_ptr -> nx_ip_http_proxy_authentication),
                                           &(ip_ptr -> nx_ip_http_proxy_authentication_length));

        /* Check status.  */
        if (status)
        {
            return(status);
        }
    }
    else
    {
        ip_ptr -> nx_ip_http_proxy_authentication_length = 0;
    }

    /* Set HTTP proxy as enabled.  */
    ip_ptr -> nx_ip_http_proxy_enable = NX_TRUE;

    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_http_proxy_client_initialize                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Wenhui Xie, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function initializes the state of HTTP Proxy and stores the    */
/*    the information of remote server.                                   */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to TCP client socket  */
/*    server_ip                             IP address of remote server   */
/*    server_port                           Port of remote server         */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NONE                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    COPY_IPV6_ADDRESS                     Copy IPv6 address             */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nxd_tcp_client_socket_connect        Connect TCP client socket     */
/*                                                                        */
/**************************************************************************/
VOID _nx_http_proxy_client_initialize(NX_TCP_SOCKET *socket_ptr, NXD_ADDRESS **server_ip, UINT *server_port)
{

NX_IP *ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;

    /* Store the server's IP and port for sending CONNECT request later.  */
    socket_ptr -> nx_tcp_socket_original_server_ip.nxd_ip_version = (*server_ip) -> nxd_ip_version;

#ifndef NX_DISABLE_IPV4
    if ((*server_ip) -> nxd_ip_version == NX_IP_VERSION_V4)
    {

        /* Copy the IPv4 address */
        socket_ptr -> nx_tcp_socket_original_server_ip.nxd_ip_address.v4 = (*server_ip) -> nxd_ip_address.v4;
    }
#endif /* !NX_DISABLE_IPV4  */

#ifdef FEATURE_NX_IPV6
    if ((*server_ip) -> nxd_ip_version == NX_IP_VERSION_V6)
    {

        /* Copy the IPv6 address */
        COPY_IPV6_ADDRESS((*server_ip) -> nxd_ip_address.v6,
                          socket_ptr -> nx_tcp_socket_original_server_ip.nxd_ip_address.v6);
    }
#endif /* FEATURE_NX_IPV6 */

    socket_ptr -> nx_tcp_socket_original_server_port = *server_port;

    /* Replace the peer info with HTTP proxy's IP and port.  */
    *server_ip = &(ip_ptr -> nx_ip_http_proxy_ip_address);
    *server_port = ip_ptr -> nx_ip_http_proxy_port;

    /* Initialize the state.  */
    socket_ptr -> nx_tcp_socket_http_proxy_state = NX_HTTP_PROXY_STATE_INIT;
    socket_ptr -> nx_tcp_socket_http_proxy_header_packet = NX_NULL;
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_http_proxy_client_connect                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Wenhui Xie, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends HTTP request to connect with HTTP Proxy server. */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to TCP client socket  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_tcp_socket_disconnect             Disconnect TCP socket         */
/*    _nx_utility_uint_to_string            Convert integer to string     */
/*    _nx_utility_base64_encode             Base64 encode                 */
/*    _nx_packet_allocate                   Allocate packet               */
/*    _nx_packet_data_append                Append packet data            */
/*    _nx_packet_release                    Release packet                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_tcp_socket_state_syn_received     Process SYN RECEIVED state    */
/*    _nx_tcp_socket_state_syn_sent         Process SYN SENT state        */
/*                                                                        */
/**************************************************************************/
UINT _nx_http_proxy_client_connect(NX_TCP_SOCKET *socket_ptr)
{

UINT status;
NX_PACKET *packet_ptr;
NX_IP *ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
NX_PACKET_POOL *pool_ptr = ip_ptr -> nx_ip_default_packet_pool;
CHAR *ip_string_ptr;
UINT ip_string_len = 0;
CHAR *port_string_ptr;
UINT port_string_len = 0;
UINT temp_length = 0;
NXD_ADDRESS *server_ip = &(socket_ptr -> nx_tcp_socket_original_server_ip);
UINT port = socket_ptr -> nx_tcp_socket_original_server_port;


    /* Check the IP version of the server.  */
    if (server_ip -> nxd_ip_version != NX_IP_VERSION_V4)
    {

        /* Only IPv4 is supported.  */
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(NX_NOT_SUPPORTED);
    }

    /* Allocate packet.  */
    if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
    {
        status =  _nx_packet_allocate(pool_ptr, &packet_ptr, NX_IPv4_TCP_PACKET, NX_NO_WAIT);
    }
    else
    {
        status =  _nx_packet_allocate(pool_ptr, &packet_ptr, NX_IPv6_TCP_PACKET, NX_NO_WAIT);
    }

    /* Check status.  */
    if (status)
    {
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(status);
    }

    /* Convert IP address and port to string.  */
    /* Use the buffer before prepend pointer to store the IP and port string.  */
    ip_string_ptr = (CHAR *)packet_ptr -> nx_packet_data_start;
    temp_length = (UINT)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start);

    /* Convert IP address to string.  */
    ip_string_len = _nx_utility_uint_to_string((UCHAR)(server_ip -> nxd_ip_address.v4 >> 24), 10, ip_string_ptr, temp_length);
    ip_string_ptr[ip_string_len++] = '.';
    ip_string_len += _nx_utility_uint_to_string((UCHAR)(server_ip -> nxd_ip_address.v4 >> 16), 10, ip_string_ptr + ip_string_len, temp_length - ip_string_len);
    ip_string_ptr[ip_string_len++] = '.';
    ip_string_len += _nx_utility_uint_to_string((UCHAR)(server_ip -> nxd_ip_address.v4 >> 8), 10, ip_string_ptr + ip_string_len, temp_length - ip_string_len);
    ip_string_ptr[ip_string_len++] = '.';
    ip_string_len += _nx_utility_uint_to_string((UCHAR)(server_ip -> nxd_ip_address.v4), 10, ip_string_ptr + ip_string_len, temp_length - ip_string_len);

    /* Convert port to string.  */
    port_string_ptr = ip_string_ptr + ip_string_len;
    port_string_len = _nx_utility_uint_to_string(port, 10, ip_string_ptr + ip_string_len, temp_length - ip_string_len);

    /* HTTP proxy opening handshake message format:
       CONNECT IP address:port HTTP/1.1
       Host: IP address
    */

    /* Build the CONNECT request: CONNECT + Request URI(IP address:port) + HTTP version.  */
    status = _nx_packet_data_append(packet_ptr, "CONNECT ", sizeof("CONNECT ") - 1, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, ip_string_ptr, ip_string_len, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, ":", sizeof(":") - 1, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, port_string_ptr, port_string_len, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, " HTTP/1.1", sizeof(" HTTP/1.1") - 1, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, NX_HTTP_CRLF, NX_HTTP_CRLF_SIZE, pool_ptr, NX_NO_WAIT);

    /* Place the Host in the header.  */
    status += _nx_packet_data_append(packet_ptr, "Host: ", sizeof("Host: ") - 1, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, ip_string_ptr, ip_string_len, pool_ptr, NX_NO_WAIT);
    status += _nx_packet_data_append(packet_ptr, NX_HTTP_CRLF, NX_HTTP_CRLF_SIZE, pool_ptr, NX_NO_WAIT);

    /* Check status.  */
    if (status)
    {
        _nx_packet_release(packet_ptr);
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(NX_NOT_SUCCESSFUL);
    }

    /* Determine if basic authentication is required.  */
    if (ip_ptr -> nx_ip_http_proxy_authentication_length)
    {

        /* Yes, attempt to build basic authentication.  */
        status = _nx_packet_data_append(packet_ptr, "Proxy-authorization: Basic ", 27, pool_ptr, NX_NO_WAIT);

        /* Check status.  */
        if (status)
        {
            _nx_packet_release(packet_ptr);
            _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
            return(status);
        }

        /* Append authentication string.  */
        status = _nx_packet_data_append(packet_ptr, ip_ptr -> nx_ip_http_proxy_authentication,
                                        ip_ptr -> nx_ip_http_proxy_authentication_length, pool_ptr, NX_NO_WAIT);
        status += _nx_packet_data_append(packet_ptr, NX_HTTP_CRLF, NX_HTTP_CRLF_SIZE, pool_ptr, NX_NO_WAIT);

        /* Check status.  */
        if (status)
        {
            _nx_packet_release(packet_ptr);
            _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
            return(NX_NOT_SUCCESSFUL);
        }
    }

    /* Append CRLF.  */
    status = _nx_packet_data_append(packet_ptr, NX_HTTP_CRLF, NX_HTTP_CRLF_SIZE, pool_ptr, NX_NO_WAIT);

    /* Check status.  */
    if (status)
    {
        _nx_packet_release(packet_ptr);
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(status);
    }

    /* Send out the packet.  */
    status = _nx_tcp_socket_send(socket_ptr, packet_ptr, NX_NO_WAIT);

    /* Check status.  */
    if (status)
    {
        _nx_packet_release(packet_ptr);
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(status);
    }

    /* Update HTTP Proxy state.  */
    socket_ptr -> nx_tcp_socket_http_proxy_state = NX_HTTP_PROXY_STATE_CONNECTING;

    /* Return successful completion.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_http_proxy_client_connect_response_process      PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Wenhui Xie, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function processes the CONNECT response from HTTP Proxy server.*/
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to TCP client socket  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_tcp_socket_receive                Receive TCP socket            */
/*    _nx_tcp_socket_disconnect             Disconnect TCP socket         */
/*    _nx_tcp_socket_thread_resume          Resume the suspended thread   */
/*    _nx_packet_data_append                Append packet data            */
/*    _nx_packet_release                    Release packet                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_tcp_socket_packet_process         Process socket packet         */
/*                                                                        */
/**************************************************************************/
UINT _nx_http_proxy_client_connect_response_process(NX_TCP_SOCKET *socket_ptr)
{
NX_PACKET *head_packet_ptr = socket_ptr -> nx_tcp_socket_http_proxy_header_packet;
NX_PACKET *new_packet_ptr;
CHAR *buffer_ptr;
UINT status = NX_SUCCESS;
NX_PACKET *work_ptr;
UINT crlf_found = 0;
NX_PACKET *tmp_ptr;
NX_IP *ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
NX_PACKET_POOL *pool_ptr = ip_ptr -> nx_ip_default_packet_pool;


    if (head_packet_ptr == NX_NULL)
    {

        /* Wait for a response from HTTP proxy.  */
        status = _nx_tcp_socket_receive(socket_ptr, &head_packet_ptr, NX_NO_WAIT);

        /* Check the return status.  */
        if (status == NX_NO_PACKET)
        {
            return(NX_IN_PROGRESS);
        } 
        else if (status != NX_SUCCESS)
        {

            /* Return an error condition.  */
            _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
            return(status);
        }

        socket_ptr -> nx_tcp_socket_http_proxy_header_packet = head_packet_ptr;
    }

    crlf_found = 0;
    work_ptr = head_packet_ptr;
    
    /* Build a pointer to the buffer area.  */
    buffer_ptr =  (CHAR *) work_ptr -> nx_packet_prepend_ptr;
    
    do
    {
    
        /* See if there is a blank line present in the buffer.  */
        /* Search the buffer for a cr/lf pair.  */
        while ((buffer_ptr < (CHAR *) work_ptr -> nx_packet_append_ptr) &&
               (crlf_found < 4))
        {
            if (!(crlf_found & 1) && (*buffer_ptr == (CHAR)13))
            {

                /* Found CR.  */
                crlf_found++;
            }
            else if((crlf_found & 1) && (*buffer_ptr == (CHAR)10))
            {

                /* Found LF.  */
                crlf_found++;
            }
            else
            {

                /* Reset the CRLF marker.  */
                crlf_found = 0;
            }
    
            /* Move the buffer pointer up.  */
            buffer_ptr++;
        }

        if (crlf_found == 4)
        {

            /* Yes, we have found the end of the HTTP response header.  */
            break;
        }

        /* Determine if the packet has already overflowed into another packet.  */

#ifndef NX_DISABLE_PACKET_CHAIN

        if (work_ptr -> nx_packet_next != NX_NULL)
        {

            /* Get the next packet in the chain.  */
            work_ptr  = work_ptr -> nx_packet_next;
            buffer_ptr =  (CHAR *) work_ptr -> nx_packet_prepend_ptr; 
        }
        else
#endif
        {
            /* Receive another packet from the HTTP proxy.  */
            status = _nx_tcp_socket_receive(socket_ptr, &new_packet_ptr, NX_NO_WAIT);
            
            /* Check the return status.  */
            if (status == NX_NO_PACKET)
            {
                return(NX_IN_PROGRESS);
            }
            else if (status != NX_SUCCESS)
            {
                break;
            }

            /* Successfully received another packet. Its contents now need to be placed in the head packet.  */
            tmp_ptr = new_packet_ptr;

#ifndef NX_DISABLE_PACKET_CHAIN
            while (tmp_ptr)
#endif /* NX_DISABLE_PACKET_CHAIN */
            {

                /* Copy the contents of the current packet into the head packet.  */
                status =  _nx_packet_data_append(head_packet_ptr, (VOID *) tmp_ptr -> nx_packet_prepend_ptr,
                                                (ULONG)(tmp_ptr -> nx_packet_append_ptr - tmp_ptr -> nx_packet_prepend_ptr),
                                                pool_ptr, NX_NO_WAIT);

#ifndef NX_DISABLE_PACKET_CHAIN

                /* Determine if an error occurred.  */
                if (status != NX_SUCCESS)
                {
                    break;
                }
                else
                {
                    tmp_ptr = tmp_ptr -> nx_packet_next;
                }
#endif /* NX_DISABLE_PACKET_CHAIN */
            }

            /* Release the new packet.  */
            _nx_packet_release(new_packet_ptr);
        }

    } while (status == NX_SUCCESS);

    if (status)
    {

        /* Return an error condition.  */
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(status);
    }

    /* Check the packet length and response code.  */
    if ((head_packet_ptr -> nx_packet_append_ptr - head_packet_ptr -> nx_packet_prepend_ptr < 12) ||
        (*(head_packet_ptr -> nx_packet_prepend_ptr + 9) != '2'))
    {

        /* If error occurs, disconnect the TCP connection.  */
        _nx_tcp_socket_disconnect(socket_ptr, NX_NO_WAIT);
        return(NX_NOT_SUCCESSFUL);
    }

    /* Update the HTTP Proxy state.  */
    socket_ptr -> nx_tcp_socket_http_proxy_state = NX_HTTP_PROXY_STATE_CONNECTED;

    /* Release the packet.  */
    _nx_packet_release(head_packet_ptr);
    socket_ptr -> nx_tcp_socket_http_proxy_header_packet = NX_NULL;

#ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT

    /* Is a connection completion callback registered with the TCP socket?  */
    if (socket_ptr -> nx_tcp_establish_notify)
    {

        /* Call the application's establish callback function.  */
        (socket_ptr -> nx_tcp_establish_notify)(socket_ptr);
    }
#endif /* NX_DISABLE_EXTENDED_NOTIFY_SUPPORT */

    /* Determine if we need to wake a thread suspended on the connection.  */
    if (socket_ptr -> nx_tcp_socket_connect_suspended_thread)
    {

        /* Resume the suspended thread.  */
        _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_connect_suspended_thread), NX_SUCCESS);
    }

    /* Return status.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_http_proxy_client_cleanup                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Wenhui Xie, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function cleans up the HTTP Proxy by resetting the state and   */
/*    release the processing packet.                                      */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to TCP client socket  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_packet_release                    Release packet                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_tcp_socket_block_cleanup          Clean up the socket block     */
/*                                                                        */
/**************************************************************************/
VOID _nx_http_proxy_client_cleanup(NX_TCP_SOCKET *socket_ptr)
{

    /* Clear the HTTP Proxy connection state.  */
    socket_ptr -> nx_tcp_socket_http_proxy_state = NX_HTTP_PROXY_STATE_INIT;

    /* Release HTTP Proxy header packet.  */
    if (socket_ptr -> nx_tcp_socket_http_proxy_header_packet)
    {
        _nx_packet_release(socket_ptr -> nx_tcp_socket_http_proxy_header_packet);
        socket_ptr -> nx_tcp_socket_http_proxy_header_packet = NX_NULL;
    }
}
#endif /* NX_ENABLE_HTTP_PROXY */