File: http.cc

package info (click to toggle)
aegis 4.24.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 34,056 kB
  • ctags: 12,500
  • sloc: cpp: 178,528; sh: 79,948; makefile: 34,813; yacc: 4,610; perl: 1,499; ansic: 492; awk: 325
file content (571 lines) | stat: -rw-r--r-- 13,433 bytes parent folder | download
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
//
//      aegis - project change supervisor
//      Copyright (C) 2003-2008 Peter Miller
//      Copyright (C) 2007 Walter Franzini
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 3 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program. If not, see
//      <http://www.gnu.org/licenses/>.
//

#include <common/ac/ctype.h>
#include <common/ac/errno.h>
#include <common/ac/stdarg.h>
#include <common/ac/stdio.h>
#include <common/ac/stdlib.h>
#include <common/ac/string.h>
#include <common/ac/time.h>

#include <common/error.h>
#include <common/now.h>
#include <common/nstring.h>
#include <common/progname.h>
#include <common/str.h>
#include <common/str_list.h>
#include <common/uuidentifier.h>
#include <common/uuidentifier/translate.h>
#include <common/version_stmp.h>
#include <libaegis/change.h>
#include <libaegis/emit/project.h>
#include <libaegis/http.h>
#include <libaegis/os.h>
#include <libaegis/project.h>


bool http_fatal_noerror;


void
http_fatal(http_error_t oops, const char *fmt, ...)
{
    va_list         ap;

    va_start(ap, fmt);
    if (oops != http_error_ok && !http_fatal_noerror)
	printf("Status: %d Error\n", oops);
    printf("Content-Type: text/html\n\n");
    printf("<html><head><title>Error</title></head><body><h1>Error</h1>\n");
    printf("The request failed because:\n<em>");
    vprintf(fmt, ap);
    va_end(ap);
    printf("</em>\n</body></html>\n");
    exit(0);
}


const char *
http_getenv(const char *name)
{
    char            *result;

    result = getenv(name);
    if (!result)
    {
	http_fatal
       	(
	    http_error_internal_server,
	    "Environment variable $%s not set.",
	    name
	);
    }
    return result;
}


static const char *
http_getenv(const char *name, const char *dflt)
{
    char *result = getenv(name);
    if (!result)
        return dflt;
    return result;
}


void
html_escape_charstar(const char *s)
{
    html_escape_string(nstring(s));
}


void
html_escape_string(string_ty *s)
{
    html_escape_charstar(s->str_text);
}


void
html_escape_string(const nstring &s)
{
    fputs(s.url_quote().c_str(), stdout);
}


void
html_encode_charstar(const char *s)
{
    html_encode_string(nstring(s));
}


void
html_encode_string(string_ty *s)
{
    html_encode_charstar(s->str_text);
}


void
html_encode_string(const nstring &s)
{
    fputs(s.html_quote(true).c_str(), stdout);
}


void
http_content_type_header(string_ty *filename)
{
    os_become_orig();
    nstring content_type = os_magic_file(filename);
    os_become_undo();
    assert(!content_type.empty());
    printf("Content-Type: %s\n", content_type.c_str());
}


static void
emit_project_attribute(project_ty *pp, change::pointer cp, const char *cname)
{
    if (!pp && cp)
        pp = cp->pp;
    if (pp && (!cp || cp->bogus))
        cp = pp->change_get();
    if (!cp)
        return;
    pconf_ty *pconf_data = change_pconf_get(cp, 0);
    if (!pconf_data->project_specific)
        return;
    string_ty *name = str_from_c(cname);
    for (size_t j = 0; j < pconf_data->project_specific->length; ++j)
    {
        attributes_ty *ap = pconf_data->project_specific->list[j];
        if
        (
            ap->name
        &&
            ap->value
        &&
            0 == strcasecmp(name->str_text, ap->name->str_text)
        )
        {
            printf("%s\n", ap->value->str_text);
        }
    }
    str_free(name);
}


void
html_header_ps(project_ty *pp, change::pointer cp)
{
    emit_project_attribute(pp, cp, "html:body-begin");
}


void
html_footer(project_ty *pp, change::pointer cp)
{
    time_t tmp = now();
    printf("<hr>\n");
    printf("This page was generated by <em>%s</em>\n", progname_get());
    printf("version %s\n", version_stamp());
    char buffer[BUFSIZ];
    // gcc 3.3 doesn't like %c
    if (strftime(buffer, BUFSIZ, "%a %d %b %Y %H:%M:%S %Z", localtime(&tmp)))
	printf("on %s.\n", buffer);
    else
	printf("on %.24s.\n", ctime(&tmp));
    emit_project_attribute(pp, cp, "html:body-end");
    printf("</body></html>\n");
}


static void
emit_project_stylesheet(project_ty *pp)
{
    //
    // Netscape 4.x has numerous CSS bugs, two of which need mentioning.
    // 1. If a style sheet is not present Netscape says 404 Not found, when
    // it should silently ignore it.  2. Style sheets who's media is not
    // "screen" will be ignored.  Fortunately we can use (2) to get around (1).
    //
    if (pp && !pp->is_a_trunk())
        emit_project_stylesheet(pp->parent_get());
    else
    {
        printf("<style type=\"text/css\">\n"
            "tr.even-group { background-color: #CCCCCC; }\n"
            "body { background-color: white; }\n"
            "</style>\n");
        printf("<link rel=\"stylesheet\" type=\"text/css\" "
            "href=\"/aedefault.css\" media=\"all\">\n");
    }
    if (pp)
    {
        printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"/");
        html_escape_string(project_name_get(pp));
        printf(".css\" media=\"all\">\n");
    }
}


void
html_header(project_ty *pp, change::pointer cp)
{
    if (cp && !pp)
        pp = cp->pp;

    printf("Content-Type: text/html\n\n");
    printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\""
        "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
    printf("<html><head>\n");
    printf("<meta name=\"ROBOTS\" content=\"NOINDEX, NOFOLLOW\">\n");
    printf("<meta name=\"GENERATOR\" content=\"aegis-%s\">\n", version_stamp());
    printf("<meta http-equiv=\"Content-Type\" "
        "content=\"text/html; charset=ISO-8859-1\">\n");
    emit_project_stylesheet(pp);
    emit_project_attribute(pp, cp, "html:meta");
}


void
emit_change(change::pointer cp)
{
    long            n;

    emit_project(cp->pp);
    if (cp->bogus)
        return;
    printf(",<br>\nChange <a href=\"%s/", http_script_name());
    html_escape_string(project_name_get(cp->pp));
    n = magic_zero_decode(cp->number);
    printf(".C%ld/?menu\">%ld</a>", n, n);
}


void
emit_change_but1(change::pointer cp)
{
    if (cp->bogus)
        emit_project_but1(cp->pp);
    else
    {
        emit_project(cp->pp);
        printf(",<br>\nChange %ld", magic_zero_decode(cp->number));
    }
}


const char *
http_script_name(void)
{
    //
    // We cache the results, so this fairly expensive operation is only
    // done once.
    //
    static nstring result;
    if (result.empty())
    {
        //
        // Get the script name.  It must exist.
        //
        nstring script_name = http_getenv("SCRIPT_NAME", "/cgi-bin/aeget");

        //
        // Get the server name.  Default it if it doesn't exist.
        //
        nstring server_name = http_getenv("SERVER_NAME", "localhost");

        //
        // Get the server port.  Default it if it doesn't exist.
        // Make sure that it is a number and is in range.
        //
        nstring server_port = http_getenv("SERVER_PORT", "80");
        int port = atoi(server_port.c_str());
        if (port <= 0 || port >= 0x10000)
        {
	    http_fatal
    	    (
		http_error_internal_server,
		"SERVER_PORT of \"%s\" not known",
		server_port.c_str()
	    );
        }

        //
        // Get the server protocol.  Default it if it does not exist.
        // We only grok HTTP, so barf if it's anything alse.
        //
        nstring server_protocol = http_getenv("SERVER_PROTOCOL", "HTTP/1.1");
        if (!server_protocol.upcase().starts_with("HTTP"))
        {
            http_fatal
            (
		http_error_internal_server,
                "SERVER_PROTOCOL of \"%s\" not known",
                server_protocol.c_str()
            );
        }

        //
        // Get the request scheme.  Default to http://.
        //
        const char *script_uri = getenv("SCRIPT_URI");
        if (!script_uri)
            script_uri = "";
        nstring request_scheme = "http";
        const char *colon = strchr(script_uri, ':');
        if (colon)
            request_scheme = nstring(script_uri, colon - script_uri);

        //
        // Assemble the script name from the various components to
        // include the protocol, host and port sections as well as the
        // script itself.
        //
        result =
            (
                request_scheme + "://"
            +
                server_name
            +
                (port == 80 ? "" : nstring::format(":%d", port))
            +
                script_name
            );
    }
    return result.c_str();
}


void
emit_project_href(project_ty *pp)
{
    printf("<a href=\"%s/", http_script_name());
    html_escape_string(project_name_get(pp));
    printf("/\">");
}


void
emit_project_href(project_ty *pp, const char *modifier, ...)
{
    assert(modifier);
    va_list ap;
    va_start(ap, modifier);
    char buffer[1000];
    vsnprintf(buffer, sizeof(buffer), modifier, ap);
    va_end(ap);

    printf("<a href=\"%s/", http_script_name());
    html_escape_string(project_name_get(pp));
    printf("/");
    if (buffer[0])
        printf("?%s", buffer);
    printf("\">");
}


void
emit_change_href_n(project_ty *pp, long n, const char *modifier)
{
    printf("<a href=\"%s/", http_script_name());
    html_escape_string(project_name_get(pp));
    printf(".C%ld/", n);
    if (modifier && *modifier)
        printf("?%s", modifier);
    printf("\">");
}


void
emit_change_href(change::pointer cp, const char *modifier)
{
    if (cp->bogus)
        emit_project_href(cp->pp,"%s", modifier);
    else
        emit_change_href_n(cp->pp, magic_zero_decode(cp->number), modifier);
}


void
emit_change_uuid_href(change::pointer cp, const nstring &uuid,
    const nstring &prefix, const nstring &suffix)
{
    if (cp->bogus)
        emit_project_href(cp->pp, "menu");
    else
    {
        assert(universal_unique_identifier_valid(cp->uuid_get()));
        printf
        (
            "<a href=\"%s%s%s\">",
            prefix.c_str(),
            uuid_translate(uuid.c_str()).c_str(),
            suffix.c_str()
        );
    }
}

void
emit_file_href(change::pointer cp, const nstring &filename,
    const char *modifier)
{
    printf("<a href=\"%s/", http_script_name());
    html_escape_string(project_name_get(cp->pp));
    if (!cp->bogus)
        printf(".C%ld", magic_zero_decode(cp->number));
    printf("/");
    html_escape_string(filename);
    if (modifier && *modifier)
        printf("?%s", modifier);
    printf("\">");
}


void
emit_file_href(change::pointer cp, string_ty *filename, const char *modifier)
{
    emit_file_href(cp, nstring(filename), modifier);
}


void
emit_rect_image(int width, int height, const char *label, int hspace)
{
    if (width < 3)
        width = 3;
    if (height < 3)
        height = 3;
    printf
    (
        "<img src=\"%s/?rect+width=%d+height=%d",
        http_script_name(),
        width,
        height
    );
    if (label && *label)
    {
        printf("+label=");
        html_escape_charstar(label);
    }
    printf("\" width=%d height=%d", width, height);
    if (hspace >= 0)
    {
#ifndef USE_STYLES
        printf(" hspace=%d", hspace);
#else
        printf(" style=\"left:%d;right:%d;\"", hspace, hspace);
#endif
    }
    printf(">");
}


void
emit_rect_image_rgb(int width, int height, const char *color, int hspace)
{
    if (width < 3)
        width = 3;
    if (height < 3)
        height = 3;
    printf
    (
        "<img\nsrc=\"%s/?rect+width=%d+height=%d",
        http_script_name(),
        width,
        height
    );
    if (color && *color)
        printf("+color=%s", color);
    printf("\" width=%d height=%d", width, height);
    if (hspace >= 0)
    {
#ifndef USE_STYLES
        printf(" hspace=%d", hspace);
#else
        printf(" style=\"left:%d;right:%d;\"", hspace, hspace);
#endif
    }
    printf(">");
}


bool
modifier_test(string_list_ty *modifiers, const char *name)
{
    for (size_t j = 0; j < modifiers->nstrings; ++j)
    {
        if (0 == strcasecmp(modifiers->string[j]->str_text, name))
        {
            return true;
        }
    }
    return false;
}


bool
modifier_test_and_clear(string_list_ty *modifiers, const char *name)
{
    for (size_t j = 0; j < modifiers->nstrings; ++j)
    {
        if (0 == strcasecmp(modifiers->string[j]->str_text, name))
        {
            modifiers->remove(modifiers->string[j]);
            return true;
        }
    }
    return false;
}


void
emit_rss_icon_with_link(project_ty *pp, const nstring &rss_filename)
{
    printf
    (
	"<a href=\"%s/%s/?rss+%s\"><img src=\"%s/icon/rss.gif\" border=0 "
	    "alt=\"RSS\"></a>\n",
	http_script_name(),
	project_name_get(pp)->str_text,
	rss_filename.c_str(),
	http_script_name()
    );
}


void
emit_rss_meta_data(project_ty *pp, const nstring &rss_filename)
{
    printf
    (
	"<link rel=\"alternate\" type=\"application/rss+xml\" "
	    "href=\"%s/%s/?rss+%s\"/>\n",
	http_script_name(),
	pp->name_get()->str_text,
	rss_filename.c_str()
    );
}