Debian Codegraph @

terps/tads/tads3/test/data/sendhttpreq.t

raw
1 #include <tads.h>
2 #include <tadsnet.h>
3 #include <file.h>
4
5 main(args)
6 {
7 if (args.length() < 2)
8 {
9 "usage: t3run sendhttpreq &lt;url&gt;\n";
10 return;
11 }
12
13 local headers = [
14 'X-TADS-Client: sendhttpreq',
15 'X-TADS-Test-1: One',
16 'X-TADS-Test-2: Two',
17 'X-TADS-Test-3: Three'
18 ];
19
20 "Sending request...\n";
21 sendNetRequest('RequestOne', args[2], 'GET', 0, headers.join('\r\n'));
22
23 "Awaiting reply...";
24 for (local done = nil ; !done ; )
25 {
26 flushOutput();
27 local evt = getNetEvent(1000);
28 switch (evt.evType)
29 {
30 case NetEvTimeout:
31 ".";
32 break;
33
34 case NetEvReply:
35 "\n***** Got result for request <<evt.requestID>>! *****\n";
36 "--- HTTP status line = <<evt.httpStatusLine>>
37 (code=<<evt.statusCode>>)\n";
38 if (evt.statusCode > 0)
39 {
40 showBody(evt.replyBody);
41 "--- Headers ---\n<<
42 evt.replyHeaders.forEachAssoc(function(name, val) {
43 tadsSay('\t' + name + '=' + val.htmlify() + '\n');
44 })>>\b";
45 "--- Raw Headers ---\n<<evt.replyHeadersRaw.htmlify()>>\b";
46 }
47 done = true;
48 break;
49 }
50 }
51
52 "Done\n";
53 }
54
55 showBody(f)
56 {
57 if (f == nil)
58 {
59 "--- No Content ---\n";
60 }
61 else if (f.getFileMode() == FileModeText)
62 {
63 "--- Content (Text) ---\n";
64 local txt;
65 while ((txt = f.readFile()) != nil)
66 "<<txt.htmlify()>>\n";
67 "\b";
68 }
69 else
70 {
71 "--- Content (Binary) ---\n";
72 local txt;
73 local addr = 0;
74 for ( ; ; addr += 32)
75 {
76 txt = f.unpackBytes('H29*!');
77 if (txt.length() == 0 || txt[1] == '')
78 break;
79 tadsSay(sprintf('%08x\ \ %s\n', addr, txt[1]));
80 }
81 "\b";
82 }
83 }