Module: Hyde::Util

Defined in:
lib/hyde/util/lookup.rb,
lib/hyde/util/html.rb,
lib/hyde/util/query.rb,
lib/hyde/util/header.rb,
lib/hyde/util/sorting.rb,
lib/hyde/util/multipart.rb

Overview

Various things that exists for purely logical reasons

Defined Under Namespace

Modules: ParserCommon Classes: FormPart, Lookup, MultipartParser, Query

Constant Summary collapse

HTTP_STATUS =

HTTP status codes and descriptions Taken from WEBrick https://github.com/ruby/webrick/blob/master/lib/webrick/httpstatus.rb

{
  100 => 'Continue',
  101 => 'Switching Protocols',
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  207 => 'Multi-Status',
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  307 => 'Temporary Redirect',
  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Large',
  415 => 'Unsupported Media Type',
  416 => 'Request Range Not Satisfiable',
  417 => 'Expectation Failed',
  418 => "I'm a teapot",
  422 => 'Unprocessable Entity',
  423 => 'Locked',
  424 => 'Failed Dependency',
  426 => 'Upgrade Required',
  428 => 'Precondition Required',
  429 => 'Too Many Requests',
  431 => 'Request Header Fields Too Large',
  451 => 'Unavailable For Legal Reasons',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout',
  505 => 'HTTP Version Not Supported',
  507 => 'Insufficient Storage',
  511 => 'Network Authentication Required'
}.freeze

Class Method Summary collapse

Class Method Details

.default_error_page(code, backtrace) ⇒ String

Default error page for Hyde

Parameters:

  • code (Integer)

    HTTP Status code

  • backtrace (Array(String), nil)

    Message to show in backtrace window

Returns:

  • (String)


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
# File 'lib/hyde/util/html.rb', line 87

def self.default_error_page(code, backtrace)
  backtrace ||= []
  errortext = HTTP_STATUS[code]
  <<~HTMLEOF
    <!DOCTYPE HTML>
    <html>
        <head>
            <title>#{Util.escape_html(errortext)}</title>
            <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>
            <meta charset="utf-8">
        </head>
        <body>
            <div class="header">
                <p class="title">HYDE</p>
                <p style="float: right"><a href="https://adastra7.net/git/yessiest/hyde">Source code</a></p>
            </div>
            <div style="padding: 0.5rem">
                <p class="text">#{Util.escape_html(errortext)}</p>
                <pre><code class="text code">
    #{backtrace.map(&Util.method(:escape_html)).join('<br/>')}
                </code></pre>
                <hr/>
                <p class="small">#{Util.escape_html(Hyde::VLINE)}</p>
            </div>
        </body>
    </html>
  HTMLEOF
end

.escape_html(str) ⇒ String

Note:

Do not use this to inject untrusted input into JavaScript code

Return string with escaped HTML entities. or CSS style of the page. This function is not adequate to prevent string interpolation.

Parameters:

  • str (String)

Returns:

  • (String)


70
71
72
# File 'lib/hyde/util/html.rb', line 70

def self.escape_html(str)
  CGI.escapeHTML(str)
end

.make_value(input, opts, sep = ";") ⇒ String

Construct a parametrized header value

Parameters:

  • input (String)
  • opts (Hash)

Returns:

  • (String)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hyde/util/header.rb', line 30

def self.make_value(input, opts, sep = ";")
  unless input.match?(/^[\w!#$%&'*+-.^_`|~]*=?[^[:cntrl:]\\",;]+$/)
    raise StandardError, "input format is invalid"
  end

  output = input
  opts.each do |key, value|
    output += if value.is_a? String
                "#{sep} #{key}=#{value}"
              else
                "#{sep} #{key}"
              end
  end
  output
end

.parse_value(input, sep = ";") ⇒ Array(String, Hash)

Parse parametrized header values

Parameters:

  • input (String)
  • sep (String, Regexp) (defaults to: ";")

Returns:

  • (Array(String, Hash))


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hyde/util/header.rb', line 11

def self.parse_value(input, sep = ";")
  parts = input.split(sep).map { |x| URI.decode_uri_component(x).strip }
  base = parts.shift
  opts = parts.map do |raw|
    key, value = raw.match(/
        \A                    # beginning of string
        ([!-~&&[^=;,]]+)      # key
        (?:=([\s!-~&&[^;,]]*)|) # optional value
        \Z                    # end of sting
    /x).to_a[1..]
    [key, ((value&.match?(/^".*"$/) ? value[1..-2] : value) or true)]
  end.to_h
  [base, opts]
end

.unescape_html(str) ⇒ String

Return string with unescaped HTML entities.

Parameters:

  • str (String)

Returns:

  • (String)


77
78
79
# File 'lib/hyde/util/html.rb', line 77

def self.unescape_html(str)
  CGI.unescapeHTML(str)
end