File: protocol.bs

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (92 lines) | stat: -rw-r--r-- 1,613 bytes parent folder | download | duplicates (2)
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
use core:io;
use util:serialize;

/**
 * HTTP/HTTPS protocol.
 */
class HttpProtocol : extends core:io:Protocol, serializable {
	// Are we https?
	private Bool isSecure;

	// Create.
	init(Bool secure) {
		init { isSecure = secure; }
	}

	// Check if we are https.
	Bool secure() : inline { isSecure; }

	// Compare parts.
	Bool partEq(Str a, Str b) : override { a == b; }

	// Hash parts.
	Nat partHash(Str a) : override { a.hash(); }

	// Output.
	void toS(StrBuf to) : override {
		if (secure)
			to << "https://";
		else
			to << "http://";
	}

	// Compare.
	protected Bool isEqualTo(Protocol other) : override {
		if (!super:isEqualTo(other))
			return false;

		unless (other as HttpProtocol)
			return false;

		return secure == other.secure;
	}

	// Format an url.
	Str format(Url url) {
		StrBuf out;

		if (secure)
			out << "https:/";
		else
			out << "http:/";

		for (i, part in url) {
			out << "/";
			if (i == 0)
				out << part; // The hostname part is special, we don't want to mangle : there!
			else
				out << escapeUrl(part);
		}
		if (url.dir)
			out << "/";

		if (url as QueryUrl) {
			if (url.parameters.any) {
				out << "?";
				Bool first = true;
				for (k, v in url.parameters) {
					if (!first)
						out << "&";
					first = false;
					out << escapeUrlParam(k) << "=" << escapeUrlParam(v);
				}
			}
		}

		out.toS;
	}

	// Read an URL.
	IStream read(Url url) {
		return Client().request(url);
	}
}

// Create HTTP urls conveniently.
QueryUrl httpUrl(Str host) {
	QueryUrl(HttpProtocol(false), [host]);
}

QueryUrl httpsUrl(Str host) {
	QueryUrl(HttpProtocol(true), [host]);
}