summaryrefslogtreecommitdiffhomepage
path: root/NetEx/HttpServer.cpp
blob: 92a13b220bcb28fd0864dc298680c2b218b16db5 (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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
/*  Starshatter OpenSource Distribution
     Copyright (c) 1997-2004, Destroyer Studios LLC.
     All Rights Reserved.

     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions are met:

     * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
     * Neither the name "Destroyer Studios" nor the names of its contributors
        may be used to endorse or promote products derived from this software
        without specific prior written permission.

     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     POSSIBILITY OF SUCH DAMAGE.

     SUBSYSTEM:    NetEx.lib
     FILE:         HttpServer.cpp
     AUTHOR:       John DiCamillo


     OVERVIEW
     ========
     Network Server Pump for HTTP Server
*/


#include "MemDebug.h"
#include "HttpServer.h"
#include "NetLayer.h"

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

// +-------------------------------------------------------------------+

HttpServer::HttpServer(WORD port, int poolsize)
    : NetServer(port, poolsize)
{
    http_server_name = "Generic HttpServer 1.0";
}

HttpServer::~HttpServer()
{ }

// +--------------------------------------------------------------------+

Text
HttpServer::ProcessRequest(Text msg, const NetAddr& addr)
{
    HttpRequest    request(msg);
    HttpResponse   response;

    request.SetClientAddr(addr);

    switch (request.Method()) {
    case HttpRequest::HTTP_GET:
        if (DoGet(request, response))
            return response;

    case HttpRequest::HTTP_POST:
        if (DoPost(request, response))
            return response;

    case HttpRequest::HTTP_HEAD:
        if (DoHead(request, response))
            return response;
    }

    return ErrorResponse();
}

// +--------------------------------------------------------------------+

Text
HttpServer::GetServerName()
{
    return http_server_name;
}

void
HttpServer::SetServerName(const char* name)
{
    http_server_name = name;
}

// +--------------------------------------------------------------------+

Text
HttpServer::DefaultResponse()
{
    Text response = "HTTP/1.1 200 OK\nServer: ";
    response += http_server_name;
    response += "\nMIME-Version: 1.0\nContent-Type: text/html\nConnection: close\n\n";

    return response;
}

Text
HttpServer::ErrorResponse()
{
    Text response = "HTTP/1.1 500 Internal Server Error\nServer:";
    response += http_server_name;
    response += "\nMIME-Version: 1.0\nContent-Type: text/html\nConnection: close\n\n";

    response += "<html><head><title>";
    response += http_server_name;
    response += " Error</title></head>\n";
    response += "<body bgcolor=\"black\" text=\"white\">\n<h1>";
    response += http_server_name;
    response += "</h1>\n<p>Veruca... sweetheart... angel...  I'm not a magician!\n";
    response += "</body></html>\n\n";

    return response;
}

// +--------------------------------------------------------------------+

bool
HttpServer::DoGet(HttpRequest& request, HttpResponse& response)
{
    char buffer[1024];
    Text content;

    content  = "<html><head><title>";
    content += http_server_name;
    content += "</title></head>\n";
    content += "<body bgcolor=\"white\" text=\"black\">\n<h1>";
    content += http_server_name;
    content += "</h1>\n";
    content += "<br><h3>Client Address:</h3><p>\n";

    sprintf_s(buffer, "%d.%d.%d.%d:%d<br><br>\n",
                client_addr.B1(),
                client_addr.B2(),
                client_addr.B3(),
                client_addr.B4(),
                client_addr.Port());

    content += buffer;
    content += "<h3>Request Method:</h3><p>\n";

    switch (request.Method()) {
    case HttpRequest::HTTP_GET:
        content += "GET";
        break;

    case HttpRequest::HTTP_POST:
        content += "POST";
        break;

    case HttpRequest::HTTP_HEAD:
        content += "HEAD";
        break;

    default:
        content += "(unsupported?)";
        break;
    }

    content += "<br>\n";
    content += "<br><h3>URI Requested:</h3><p>\n";
    content += request.URI();
    content += "<br>\n";

    if (request.GetQuery().size() > 0) {
        content += "<br><h3>Query Parameters:</h3>\n";

        ListIter<HttpParam> q_iter = request.GetQuery();
        while (++q_iter) {
            HttpParam* q = q_iter.value();
            sprintf_s(buffer, "<b>%s:</b> <i>%s</i><br>\n", q->name.data(), q->value.data());
            content += buffer;
        }
    }

    content += "<br><h3>Request Headers:</h3>\n";
    ListIter<HttpParam> h_iter = request.GetHeaders();
    while (++h_iter) {
        HttpParam* h = h_iter.value();
        sprintf_s(buffer, "<b>%s:</b> <i>%s</i><br>\n", h->name.data(), h->value.data());
        content += buffer;
    }

    content += "</body></html>\n\n";

    response.SetStatus(HttpResponse::SC_OK);
    response.AddHeader("Server",        http_server_name);
    response.AddHeader("MIME-Version",  "1.0");
    response.AddHeader("Content-Type",  "text/html");
    response.SetContent(content);

    return true;
}

// +--------------------------------------------------------------------+

bool
HttpServer::DoPost(HttpRequest& request, HttpResponse& response)
{
    return DoGet(request, response);
}

// +--------------------------------------------------------------------+

bool
HttpServer::DoHead(HttpRequest& request, HttpResponse& response)
{
    if (DoGet(request, response)) {
        int len = response.Content().length();
 
        char buffer[256];
        sprintf_s(buffer, "%d", len);
        response.SetHeader("Content-Length", buffer);
        response.SetContent("");

        return true;
    }

    return false;
}

// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+

HttpRequest::HttpRequest(const char* r)
    : method(0)
{
    if (r && *r)
        ParseRequest(r);
}

HttpRequest::~HttpRequest()
{
    query.destroy();
    headers.destroy();
    cookies.destroy();
}

// +--------------------------------------------------------------------+

void
HttpRequest::ParseRequest(Text request)
{
    if (request.length() <= 8)
        return;

    const char* pReq    = 0;
    const char* pURI    = 0;
    const char* pQuery  = 0;

    switch (request[0]) {
    case 'G':   
        if (request.indexOf("GET") == 0)
            method = HTTP_GET;
        break;

    case 'P':
        if (request.indexOf("POST") == 0)
            method = HTTP_POST;
        break;

    case 'H':
        if (request.indexOf("HEAD") == 0)
            method = HTTP_HEAD;
        break;

    default:
        break;
    }

    if (!method) return;

    char buffer[1024];
    int  i = 0;

    // save the request line:
    pReq = request.data();
    while (*pReq && *pReq != '\n')
        buffer[i++] = *pReq++;
    buffer[i] = 0;

    request_line = buffer;
    i = 0;

    // find the URI:
    pURI = request.data();
    while (*pURI && !isspace(*pURI))
        pURI++;

    while (*pURI && isspace(*pURI))
        pURI++;

    // copy the URI and find the query string:
    while (*pURI && *pURI != '?' && !isspace(*pURI)) {
        buffer[i++] = *pURI++;
    }

    buffer[i] = 0;
    uri = buffer;
    pQuery = pURI;

    // parse the query string:
    if (*pQuery == '?') {
        pQuery++;

        while (*pQuery && !isspace(*pQuery)) {
            char name_buf[1024];
            char value_buf[1024];

            i = 0;
            while (*pQuery && *pQuery != '=' && !isspace(*pQuery))
                name_buf[i++] = *pQuery++;
            name_buf[i] = 0;

            if (*pQuery == '=')
                pQuery++;

            i = 0;
            while (*pQuery && *pQuery != '&' && !isspace(*pQuery))
                value_buf[i++] = *pQuery++;
            value_buf[i] = 0;

            if (*pQuery == '&')
                pQuery++;

            HttpParam* param = new(__FILE__,__LINE__) HttpParam(name_buf, DecodeParam(value_buf));
            if (param)
                query.append(param);
        }
    }

    // get the headers:
    const char* p = request.data();
    while (*p && *p != '\n')
        p++;

    if (*p == '\n') p++;

    while (*p && *p != '\r' && *p != '\n') {
        char name_buf[1024];
        char value_buf[1024];

        i = 0;
        while (*p && *p != ':')
            name_buf[i++] = *p++;
        name_buf[i] = 0;

        p++;                       // skip ':'
        while (isspace(*p)) p++;   // skip spaces

        i = 0;
        while (*p && *p != '\r' && *p != '\n') // read to end of header line
            value_buf[i++] = *p++;
        value_buf[i] = 0;

        if (!_stricmp(name_buf, "Cookie")) {
            ParseCookie(value_buf);
        }
        else {
            HttpParam* param = new(__FILE__,__LINE__) HttpParam(name_buf, value_buf);
            if (param)
                headers.append(param);
        }

        while (*p && *p != '\n')
            p++;

        if (*p == '\n') p++;
    }

    if (method == HTTP_POST && *p) {
        while (*p == '\n' || *p == '\r')
            p++;

        content = *p;
        pQuery  = p;

        while (*pQuery && !isspace(*pQuery)) {
            char name_buf[1024];
            char value_buf[1024];

            i = 0;
            while (*pQuery && *pQuery != '=' && !isspace(*pQuery))
                name_buf[i++] = *pQuery++;
            name_buf[i] = 0;

            if (*pQuery == '=')
                pQuery++;

            i = 0;
            while (*pQuery && *pQuery != '&' && !isspace(*pQuery))
                value_buf[i++] = *pQuery++;
            value_buf[i] = 0;

            if (*pQuery == '&')
                pQuery++;

            HttpParam* param = new(__FILE__,__LINE__) HttpParam(name_buf, DecodeParam(value_buf));
            if (param)
                query.append(param);
        }
    }
}

void
HttpRequest::ParseCookie(const char* param)
{
    const char* p = param;

    while (p && *p) {
        while (isspace(*p)) p++;

        // just ignore reserved attributes
        if (*p == '$') {
            while (*p && !isspace(*p) && *p != ';') p++;

            if (*p == ';')
                p++;
        }

        // found a cookie!
        else if (isalpha(*p)) {
            char name[1024];
            char data[1024];

            char* d = name;
            while (*p && *p != '=')
                *d++ = *p++;
            *d = 0;

            if (*p == '=')
                p++;

            if (*p == '"')
                p++;

            d = data;
            while (*p && *p != '"' && *p != ';')
                *d++ = *p++;
            *d = 0;

            if (*p == '"')
                p++;

            if (*p == ';')
                p++;

            HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, data);
            if (param)
                cookies.append(param);
        }

        // this shouldn't happen - abandon the parse
        else {
            return;
        }
    }
}

// +--------------------------------------------------------------------+

Text
HttpRequest::GetParam(const char* name)
{
    ListIter<HttpParam> iter = query;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name)
            return p->value;
    }

    return Text();
}

// +--------------------------------------------------------------------+

Text
HttpRequest::GetHeader(const char* name)
{
    ListIter<HttpParam> iter = headers;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name)
            return p->value;
    }

    return Text();
}

void
HttpRequest::SetHeader(const char* name, const char* value)
{
    ListIter<HttpParam> iter = headers;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            p->value = value;
            return;
        }
    }

    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        headers.append(param);
}

void
HttpRequest::AddHeader(const char* name, const char* value)
{
    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        headers.append(param);
}

// +--------------------------------------------------------------------+

Text
HttpRequest::GetCookie(const char* name)
{
    ListIter<HttpParam> iter = cookies;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name)
            return p->value;
    }

    return Text();
}

void
HttpRequest::SetCookie(const char* name, const char* value)
{
    ListIter<HttpParam> iter = cookies;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            p->value = value;
            return;
        }
    }

    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        cookies.append(param);
}

void
HttpRequest::AddCookie(const char* name, const char* value)
{
    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        cookies.append(param);
}

// +--------------------------------------------------------------------+

Text
HttpRequest::DecodeParam(const char* value)
{
    if (!value || !*value) return "";

    int   size   = strlen(value);
    char  val    = 0;
    char  code[4];
    char  sbuf[256];
    char* lbuf = 0;

    char* dst    = sbuf;
    char* p      = sbuf;

    if (size > 255) {
        lbuf   = new(__FILE__,__LINE__) char[size+1];
        dst    = lbuf;
        p      = lbuf;
    }

    if (p) {
        while (*value) {
            switch (*value) {
            default:    *p++ = *value; break;
            case '+':   *p++ = ' ';    break;

            case '%':
                value++;
                code[0] = *value++;
                code[1] = *value;
                code[2] = 0;

                val = (char) strtol(code, 0, 16);
                *p++ = val;
                break;
            }

            value++;
        }

        *p = 0;
    }

    Text  result = dst;

    if (lbuf)
        delete [] lbuf;

    return result;
}

// +--------------------------------------------------------------------+

Text
HttpRequest::EncodeParam(const char* value)
{
    if (!value || !*value) return "";

    int   size   = strlen(value);
    char  hex1   = 0;
    char  hex2   = 0;

    char  sbuf[1024];
    char* lbuf = 0;

    char* dst    = sbuf;
    char* p      = sbuf;

    if (size > 255) {
        lbuf   = new(__FILE__,__LINE__) char[4*size+1];
        dst    = lbuf;
        p      = lbuf;
    }

    if (p) {
        while (*value) {
            switch (*value) {
            default:    *p++ = *value; break;
            case ' ':   *p++ = '+';    break;

            case '?':   *p++ = '%'; *p++ = '3'; *p++ = 'F'; break;
            case '&':   *p++ = '%'; *p++ = '2'; *p++ = '6'; break;
            case ':':   *p++ = '%'; *p++ = '3'; *p++ = 'A'; break;
            case '/':   *p++ = '%'; *p++ = '2'; *p++ = 'F'; break;
            case '\\':  *p++ = '%'; *p++ = '5'; *p++ = 'C'; break;
            case '%':   *p++ = '%'; *p++ = '2'; *p++ = '5'; break;
            case '|':   *p++ = '%'; *p++ = '7'; *p++ = 'C'; break;
            case '<':   *p++ = '%'; *p++ = '3'; *p++ = 'C'; break;
            case '>':   *p++ = '%'; *p++ = '3'; *p++ = 'E'; break;
            case '[':   *p++ = '%'; *p++ = '5'; *p++ = 'B'; break;
            case ']':   *p++ = '%'; *p++ = '5'; *p++ = 'D'; break;
            case '{':   *p++ = '%'; *p++ = '7'; *p++ = 'B'; break;
            case '}':   *p++ = '%'; *p++ = '7'; *p++ = 'D'; break;
            case '"':   *p++ = '%'; *p++ = '2'; *p++ = '2'; break;
            case '^':   *p++ = '%'; *p++ = '5'; *p++ = 'E'; break;
            case '`':   *p++ = '%'; *p++ = '6'; *p++ = '0'; break;
            case '\n':  break;
            case '\r':  break;
            case '\t':  break;
            }

            value++;
        }

        *p = 0;
    }

    Text  result = dst;

    if (lbuf)
        delete [] lbuf;

    return result;
}

// +--------------------------------------------------------------------+

HttpRequest::operator Text()
{
    Text response = request_line.data();
    response += "\n";

    for (int i = 0; i < headers.size(); i++) {
        HttpParam* h = headers[i];
        response += h->name;
        response += ": ";
        response += h->value;
        response += "\n";
    }

    for (int i = 0; i < cookies.size(); i++) {
        HttpParam* c = cookies[i];
        response += "Cookie: ";
        response += c->name;
        response += "=\"";
        response += c->value;
        response += "\"\n";
    }

    response += "Connection: close\n\n";
    response += content;

    return response;
}

// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+

HttpResponse::HttpResponse(int stat, const char* data)
    : status(stat), content(data)
{ }

HttpResponse::HttpResponse(const char* r)
    : status(0), content(r)
{
    if (r && *r)
        ParseResponse(r);
}

HttpResponse::~HttpResponse()
{
    headers.destroy();
    cookies.destroy();
}

// +--------------------------------------------------------------------+

HttpResponse::operator Text()
{
    Text response;

    switch (status) {
    case SC_CONTINUE             : response = "HTTP/1.1 100 Continue\n";                break;
    case SC_SWITCHING_PROTOCOLS  : response = "HTTP/1.1 101 Switching Protocols\n";     break;

    case SC_OK                   : response = "HTTP/1.1 200 OK\n";                      break;
    case SC_CREATED              : response = "HTTP/1.1 201 Created\n";                 break;
    case SC_ACCEPTED             : response = "HTTP/1.1 202 Accepted\n";                break;
    case SC_NON_AUTHORITATIVE    : response = "HTTP/1.1 203 Non Authoritative\n";       break;
    case SC_NO_CONTENT           : response = "HTTP/1.1 204 No Content\n";              break;
    case SC_RESET_CONTENT        : response = "HTTP/1.1 205 Reset Content\n";           break;
    case SC_PARTIAL_CONTENT      : response = "HTTP/1.1 206 Partial Content\n";         break;

    case SC_MULTIPLE_CHOICES     : response = "HTTP/1.1 300 Multiple Choices\n";        break;
    case SC_MOVED_PERMANENTLY    : response = "HTTP/1.1 301 Moved Permanently\n";       break;
    case SC_FOUND                : response = "HTTP/1.1 302 Found\n";                   break;
    case SC_SEE_OTHER            : response = "HTTP/1.1 303 See Other\n";               break;
    case SC_NOT_MODIFIED         : response = "HTTP/1.1 304 Not Modified\n";            break;
    case SC_USE_PROXY            : response = "HTTP/1.1 305 Use Proxy\n";               break;
    case SC_TEMPORARY_REDIRECT   : response = "HTTP/1.1 307 Temporary Redirect\n";      break;

    case SC_BAD_REQUEST          : response = "HTTP/1.1 400 Bad Request\n";             break;
    case SC_UNAUTHORIZED         : response = "HTTP/1.1 401 Unauthorized\n";            break;
    case SC_PAYMENT_REQUIRED     : response = "HTTP/1.1 402 Payment Required\n";        break;
    case SC_FORBIDDEN            : response = "HTTP/1.1 403 Forbidden\n";               break;
    case SC_NOT_FOUND            : response = "HTTP/1.1 404 Not Found\n";               break;
    case SC_METHOD_NOT_ALLOWED   : response = "HTTP/1.1 405 Method Not Allowed\n";      break;
    case SC_NOT_ACCEPTABLE       : response = "HTTP/1.1 406 Not Acceptable\n";          break;
    case SC_PROXY_AUTH_REQ       : response = "HTTP/1.1 407 Proxy Authorization Req\n"; break;
    case SC_REQUEST_TIME_OUT     : response = "HTTP/1.1 408 Request Timeout\n";         break;
    case SC_CONFLICT             : response = "HTTP/1.1 409 Conflict\n";                break;
    case SC_GONE                 : response = "HTTP/1.1 410 Gone\n";                    break;
    case SC_LENGTH_REQUIRED      : response = "HTTP/1.1 411 Length Required\n";         break;

    default:
    case SC_SERVER_ERROR         : response = "HTTP/1.1 500 Internal Server Error\n";   break;
    case SC_NOT_IMPLEMENTED      : response = "HTTP/1.1 501 Not Implemented\n";         break;
    case SC_BAD_GATEWAY          : response = "HTTP/1.1 502 Bad Gateway\n";             break;
    case SC_SERVICE_UNAVAILABLE  : response = "HTTP/1.1 503 Service Unavailable\n";     break;
    case SC_GATEWAY_TIMEOUT      : response = "HTTP/1.1 504 Gateway Timeout\n";         break;
    case SC_VERSION_NOT_SUPPORTED: response = "HTTP/1.1 505 HTTP Version Not Supported\n"; break;
    }

    SetHeader("Connection", "close");

    char buffer[256];

    if (content.length()) {
        sprintf_s(buffer, "%d", content.length());
        SetHeader("Content-Length", buffer);
    }

    for (int i = 0; i < cookies.size(); i++) {
        HttpParam* cookie = cookies.at(i);
        sprintf_s(buffer, "%s=\"%s\"; Version=\"1\"", cookie->name.data(), cookie->value.data());

        AddHeader("Set-Cookie", buffer);
    }

    for (int i = 0; i < headers.size(); i++) {
        const HttpParam* p = headers.at(i);
        sprintf_s(buffer, "%s: %s\n", p->name.data(), p->value.data());
        response += buffer;
    }

    response += "\n";
    response += content;

    return response;
}

// +--------------------------------------------------------------------+

Text
HttpResponse::GetHeader(const char* name)
{
    ListIter<HttpParam> iter = headers;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name)
            return p->value;
    }

    return Text();
}

void
HttpResponse::SetHeader(const char* name, const char* value)
{
    ListIter<HttpParam> iter = headers;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            p->value = value;
            return;
        }
    }

    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        headers.append(param);
}

void
HttpResponse::AddHeader(const char* name, const char* value)
{
    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        headers.append(param);
}

// +--------------------------------------------------------------------+

Text
HttpResponse::GetCookie(const char* name)
{
    ListIter<HttpParam> iter = cookies;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name)
            return p->value;
    }

    return Text();
}

void
HttpResponse::SetCookie(const char* name, const char* value)
{
    ListIter<HttpParam> iter = cookies;
    while (++iter) {
        HttpParam* p = iter.value();

        if (p->name == name) {
            p->value = value;
            return;
        }
    }

    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        cookies.append(param);
}

void
HttpResponse::AddCookie(const char* name, const char* value)
{
    HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, value);
    if (param)
        cookies.append(param);
}

// +--------------------------------------------------------------------+

void
HttpResponse::SendRedirect(const char* url)
{
    status = SC_TEMPORARY_REDIRECT;
    SetHeader("Location", url);
}

// +--------------------------------------------------------------------+

void
HttpResponse::ParseResponse(Text response)
{
    if (response.length() <= 12 || response.indexOf("HTTP/1.") != 0)
        return;

    const char* pStatus = response.data() + 9;

    sscanf_s(pStatus, "%d", &status);
    if (!status) return;

    int  i = 0;

    // get the headers:
    const char* p = response.data();
    while (*p && *p != '\n')
        p++;

    if (*p == '\n') p++;

    while (*p && *p != '\r' && *p != '\n') {
        char name_buf[1024];
        char value_buf[1024];

        i = 0;
        while (*p && *p != ':')
            name_buf[i++] = *p++;
        name_buf[i] = 0;

        p++;                       // skip ':'
        while (isspace(*p)) p++;   // skip spaces

        i = 0;
        while (*p && *p != '\r' && *p != '\n') // read to end of header line
            value_buf[i++] = *p++;
        value_buf[i] = 0;

        if (!_stricmp(name_buf, "Set-Cookie")) {
            ParseCookie(value_buf);
        }
        else {
            HttpParam* param = new(__FILE__,__LINE__) HttpParam(name_buf, value_buf);
            if (param)
                headers.append(param);
        }

        while (*p && *p != '\n')
            p++;

        if (*p == '\n') p++;
    }

    if (*p == '\n') p++;
    content = p;
}

void
HttpResponse::ParseCookie(const char* param)
{
    const char* p = param;

    while (p && *p) {
        while (isspace(*p)) p++;

        // just ignore reserved attributes
        if (*p == '$') {
            while (*p && !isspace(*p) && *p != ';') p++;

            if (*p == ';')
                p++;
        }

        // found a cookie!
        else if (isalpha(*p)) {
            char name[1024];
            char data[1024];

            char* d = name;
            while (*p && *p != '=')
                *d++ = *p++;
            *d = 0;

            if (*p == '=')
                p++;

            if (*p == '"')
                p++;

            d = data;
            while (*p && *p != '"' && *p != ';')
                *d++ = *p++;
            *d = 0;

            if (*p == '"')
                p++;

            if (*p == ';')
                p++;

            // ignore the version attribute
            if (_stricmp(name, "version")) {
                HttpParam* param = new(__FILE__,__LINE__) HttpParam(name, data);
                if (param)
                    cookies.append(param);
            }
        }

        // this shouldn't happen - abandon the parse
        else {
            return;
        }
    }
}