Module: Hyde::Util

Defined in:
lib/hyde/util/lookup.rb,
lib/hyde/util/html.rb,
lib/hyde/util/query.rb,
lib/hyde/util/multipart.rb,
lib/hyde/util/parseutils.rb,
lib/hyde/util/parsesorting.rb

Overview

Various things that exists for purely logical reasons

Defined Under Namespace

Modules: HeaderRegexp, ParserCommon, ParserSorting 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)


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

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 or CSS style of the page.

Return string with escaped HTML entities. This function is not adequate to prevent string interpolation.

Parameters:

  • str (String)

Returns:

  • (String)


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

def self.escape_html(str)
  str = CGI.escapeHTML(str)
  str.gsub(/[^\x1-\x7E]/) do |match|
    "&##{match.ord};"
  end
end

.unescape_html(str) ⇒ String

Return string with unescaped HTML entities.

Parameters:

  • str (String)

Returns:

  • (String)


79
80
81
# File 'lib/hyde/util/html.rb', line 79

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