landline 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/HACKING.md +30 -0
  3. data/LAYOUT.md +59 -0
  4. data/LICENSE.md +660 -0
  5. data/README.md +159 -0
  6. data/lib/landline/dsl/constructors_path.rb +107 -0
  7. data/lib/landline/dsl/constructors_probe.rb +28 -0
  8. data/lib/landline/dsl/methods_common.rb +28 -0
  9. data/lib/landline/dsl/methods_path.rb +75 -0
  10. data/lib/landline/dsl/methods_probe.rb +129 -0
  11. data/lib/landline/dsl/methods_template.rb +16 -0
  12. data/lib/landline/node.rb +87 -0
  13. data/lib/landline/path.rb +157 -0
  14. data/lib/landline/pattern_matching/glob.rb +168 -0
  15. data/lib/landline/pattern_matching/rematch.rb +49 -0
  16. data/lib/landline/pattern_matching/util.rb +15 -0
  17. data/lib/landline/pattern_matching.rb +75 -0
  18. data/lib/landline/probe/handler.rb +56 -0
  19. data/lib/landline/probe/http_method.rb +74 -0
  20. data/lib/landline/probe/serve_handler.rb +39 -0
  21. data/lib/landline/probe.rb +62 -0
  22. data/lib/landline/request.rb +135 -0
  23. data/lib/landline/response.rb +140 -0
  24. data/lib/landline/server.rb +49 -0
  25. data/lib/landline/template/erb.rb +27 -0
  26. data/lib/landline/template/erubi.rb +36 -0
  27. data/lib/landline/template.rb +95 -0
  28. data/lib/landline/util/cookie.rb +150 -0
  29. data/lib/landline/util/errors.rb +11 -0
  30. data/lib/landline/util/html.rb +119 -0
  31. data/lib/landline/util/lookup.rb +37 -0
  32. data/lib/landline/util/mime.rb +1276 -0
  33. data/lib/landline/util/multipart.rb +175 -0
  34. data/lib/landline/util/parsesorting.rb +37 -0
  35. data/lib/landline/util/parseutils.rb +111 -0
  36. data/lib/landline/util/query.rb +66 -0
  37. data/lib/landline.rb +20 -0
  38. metadata +85 -0
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi/escape'
4
+
5
+ module Landline
6
+ module Util
7
+ # HTTP status codes and descriptions
8
+ # Taken from WEBrick {https://github.com/ruby/webrick/blob/master/lib/webrick/httpstatus.rb}
9
+ HTTP_STATUS = {
10
+ 100 => 'Continue',
11
+ 101 => 'Switching Protocols',
12
+ 200 => 'OK',
13
+ 201 => 'Created',
14
+ 202 => 'Accepted',
15
+ 203 => 'Non-Authoritative Information',
16
+ 204 => 'No Content',
17
+ 205 => 'Reset Content',
18
+ 206 => 'Partial Content',
19
+ 207 => 'Multi-Status',
20
+ 300 => 'Multiple Choices',
21
+ 301 => 'Moved Permanently',
22
+ 302 => 'Found',
23
+ 303 => 'See Other',
24
+ 304 => 'Not Modified',
25
+ 305 => 'Use Proxy',
26
+ 307 => 'Temporary Redirect',
27
+ 400 => 'Bad Request',
28
+ 401 => 'Unauthorized',
29
+ 402 => 'Payment Required',
30
+ 403 => 'Forbidden',
31
+ 404 => 'Not Found',
32
+ 405 => 'Method Not Allowed',
33
+ 406 => 'Not Acceptable',
34
+ 407 => 'Proxy Authentication Required',
35
+ 408 => 'Request Timeout',
36
+ 409 => 'Conflict',
37
+ 410 => 'Gone',
38
+ 411 => 'Length Required',
39
+ 412 => 'Precondition Failed',
40
+ 413 => 'Request Entity Too Large',
41
+ 414 => 'Request-URI Too Large',
42
+ 415 => 'Unsupported Media Type',
43
+ 416 => 'Request Range Not Satisfiable',
44
+ 417 => 'Expectation Failed',
45
+ 418 => "I'm a teapot",
46
+ 422 => 'Unprocessable Entity',
47
+ 423 => 'Locked',
48
+ 424 => 'Failed Dependency',
49
+ 426 => 'Upgrade Required',
50
+ 428 => 'Precondition Required',
51
+ 429 => 'Too Many Requests',
52
+ 431 => 'Request Header Fields Too Large',
53
+ 451 => 'Unavailable For Legal Reasons',
54
+ 500 => 'Internal Server Error',
55
+ 501 => 'Not Implemented',
56
+ 502 => 'Bad Gateway',
57
+ 503 => 'Service Unavailable',
58
+ 504 => 'Gateway Timeout',
59
+ 505 => 'HTTP Version Not Supported',
60
+ 507 => 'Insufficient Storage',
61
+ 511 => 'Network Authentication Required'
62
+ }.freeze
63
+
64
+ # Return string with escaped HTML entities.
65
+ # @note Do **not** use this to inject untrusted input into JavaScript code or CSS style of the page.
66
+ # This function is not adequate to prevent string interpolation.
67
+ # @param str [String]
68
+ # @return [String]
69
+ def self.escape_html(str)
70
+ str = CGI.escapeHTML(str)
71
+ str.gsub(/[^\x1-\x7E]/) do |match|
72
+ "&##{match.ord};"
73
+ end
74
+ end
75
+
76
+ # Return string with unescaped HTML entities.
77
+ # @param str [String]
78
+ # @return [String]
79
+ def self.unescape_html(str)
80
+ CGI.unescapeHTML(str)
81
+ end
82
+
83
+ # rubocop:disable Metrics/MethodLength
84
+
85
+ # Default error page for Landline
86
+ # @param code [Integer] HTTP Status code
87
+ # @param backtrace [Array(String), nil] Message to show in backtrace window
88
+ # @return [String]
89
+ def self.default_error_page(code, backtrace)
90
+ backtrace ||= []
91
+ errortext = HTTP_STATUS[code]
92
+ <<~HTMLEOF
93
+ <!DOCTYPE HTML>
94
+ <html>
95
+ <head>
96
+ <title>#{Util.escape_html(errortext)}</title>
97
+ <style> .header {padding: 0.5rem; overflow: auto;} .title { font-weight: bolder; font-size: 48px; margin: 10px 10px; text-shadow: 1px 1px 1px #202222, 2px 2px 2px #404444; float: left } body { margin: 0; } .text { font-size 1rem; } .small { color: #7D7D7D; font-size: 12px;} .code { font-family: monospace; font-size: 0.7rem; } </style>
98
+ <meta charset="utf-8">
99
+ </head>
100
+ <body>
101
+ <div class="header">
102
+ <p class="title">Landline</p>
103
+ <p style="float: right"><a href="https://adastra7.net/git/yessiest/landline">Source code</a></p>
104
+ </div>
105
+ <div style="padding: 0.5rem">
106
+ <p class="text">#{Util.escape_html(errortext)}</p>
107
+ <pre><code class="text code">
108
+ #{backtrace.map(&Util.method(:escape_html)).join('<br/>')}
109
+ </code></pre>
110
+ <hr/>
111
+ <p class="small">#{Util.escape_html(Landline::VLINE)}</p>
112
+ </div>
113
+ </body>
114
+ </html>
115
+ HTMLEOF
116
+ end
117
+ # rubocop:enable Metrics/MethodLength
118
+ end
119
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Landline
4
+ # Various things that exists for purely logical reasons
5
+ module Util
6
+ # Value container with recursive lookup
7
+ class Lookup
8
+ # @param parent [Lookup, nil]
9
+ def initialize(parent = nil, hash = {})
10
+ @parent = (parent or Hash.new(nil))
11
+ @storage = hash
12
+ end
13
+
14
+ # Initialize a Lookup from Hash
15
+ # @param hash [Hash]
16
+ def self.[](hash)
17
+ Lookup.new(nil, Hash[hash])
18
+ end
19
+
20
+ # Get a value by key
21
+ # @param key [#hash] key for value
22
+ # @return [Object,nil]
23
+ def [](key)
24
+ @storage[key] or @parent[key]
25
+ end
26
+
27
+ # Set a value by key
28
+ # @param key [#hash] key for value
29
+ # @param value [Object] value value
30
+ def []=(key, value)
31
+ @storage[key] = value
32
+ end
33
+
34
+ attr_accessor :parent
35
+ end
36
+ end
37
+ end