Compare commits
2 Commits
4312d31268
...
f807d5ca8d
Author | SHA1 | Date |
---|---|---|
Yessiest | f807d5ca8d | |
Yessiest | 3300f966a9 |
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
|
||||
require 'hyde'
|
||||
|
||||
app = Hyde::Server.new do
|
||||
root ENV["PWD"]
|
||||
index ["index.html"]
|
||||
post "/" do
|
||||
formdata = form if form?
|
||||
puts formdata.pretty_inspect
|
||||
erubi(file("index.rhtml"), { formdata: formdata }).run
|
||||
end
|
||||
get "/" do
|
||||
erubi(file("index.rhtml")).run
|
||||
end
|
||||
end
|
||||
|
||||
run app
|
|
@ -0,0 +1,35 @@
|
|||
<!DOCTYPE>
|
||||
<html>
|
||||
<head>
|
||||
<title>Form upload test</title>
|
||||
<style>
|
||||
.form { display: flex; flex-direction: column }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Form upload test</h1>
|
||||
<hr/>
|
||||
<p> Please enter the following details: <p>
|
||||
<form method="post"
|
||||
class="form"
|
||||
enctype="multipart/form-data">
|
||||
<input type="text" name="form_name" placeholder="Name" required>
|
||||
<input type="text" name="form_company" placeholder="Company" required>
|
||||
<input type="text" name="form_phone" placeholder="Phone" required>
|
||||
<textarea name="comment" placeholder="Comment"></textarea>
|
||||
<input id="form_files" type="file" name="form_files[]" multiple>
|
||||
<label for="form_files">Attach file</label>
|
||||
<input type="submit" value="Send form">
|
||||
</form>
|
||||
<% if (defined? formdata) and formdata %>
|
||||
<hr>
|
||||
<ul>
|
||||
<% formdata.each do |key, part| %>
|
||||
<li><%= key %>: <%= part.is_a?(String) ? part : "File: #{part.map do |x| x.filename end.join(',')}" %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
../../lib
|
|
@ -0,0 +1 @@
|
|||
Example of handling forms in Hyde
|
|
@ -6,7 +6,7 @@ module Hyde
|
|||
module ProbeConstructors
|
||||
# Create a new erb template
|
||||
# @see {Hyde::Template#new}
|
||||
def erb(input, vars = nil)
|
||||
def erb(input, vars = {})
|
||||
Hyde::Templates::ERB.new(input,
|
||||
vars,
|
||||
parent: @origin)
|
||||
|
@ -16,7 +16,7 @@ module Hyde
|
|||
# @see {Hyde::Template#new}
|
||||
# @param freeze [Boolean] whether to use frozen string literal
|
||||
# @param capture [Boolean] whether to enable output capturing
|
||||
def erubi(input, vars = nil, freeze: true, capture: false)
|
||||
def erubi(input, vars = {}, freeze: true, capture: false)
|
||||
Hyde::Templates::Erubi.new(input,
|
||||
vars,
|
||||
parent: @origin,
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'header'
|
||||
|
||||
module Hyde
|
||||
# Utility class for handling cookies
|
||||
class Cookie
|
||||
# @param data [String] raw cookie data
|
||||
def initialize(key, value, params = {})
|
||||
@key = key
|
||||
@value = value
|
||||
@params = params
|
||||
end
|
||||
|
||||
# Convert cookie to "Set-Cookie: " string representation.
|
||||
# @return [String]
|
||||
def to_s
|
||||
Hyde::Util.make_value(to_short, @params)
|
||||
end
|
||||
|
||||
# Convert cookie to "Cookie: " string representation (no params)
|
||||
def to_short
|
||||
"#{key.to_s.strip}=#{value.to_s.strip}"
|
||||
end
|
||||
|
||||
attr_accessor :key, :value
|
||||
attr_reader :params
|
||||
|
||||
# Create cookie from a "Set-Cookie: " format
|
||||
# @param data [String] value part of "Set-Cookie: " header
|
||||
# @return [Cookie]
|
||||
def self.from_setcookie_string(data)
|
||||
kvpair, params = Hyde::Util.parse_value(data)
|
||||
key, value = kvpair.split("=").map(&:strip)
|
||||
Cookie.new(key, value, params)
|
||||
end
|
||||
|
||||
# Create cookie(s) from a "Cookie: " format
|
||||
# @param data [String] value part of "Cookie: " header
|
||||
# @return [Array(Cookie)]
|
||||
def self.from_cookie_string(data)
|
||||
data.split(";").map do |cookiestr|
|
||||
Cookie.new(*cookiestr.split("=").map(&:strip))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -12,7 +12,12 @@ module Hyde
|
|||
parts = input.split(sep).map { |x| URI.decode_uri_component(x).strip }
|
||||
base = parts.shift
|
||||
opts = parts.map do |raw|
|
||||
key, value = raw.match(/^([^=]*)(?:=(.*)|)\Z/).to_a[1..]
|
||||
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]
|
||||
|
@ -24,7 +29,7 @@ module Hyde
|
|||
# @return [String]
|
||||
def self.make_value(input, opts, sep = ";")
|
||||
unless input.match?(/^[\w!#$%&'*+-.^_`|~]*=?[^[:cntrl:]\\",;]+$/)
|
||||
raise StandardError, "input contains invalid characters"
|
||||
raise StandardError, "input format is invalid"
|
||||
end
|
||||
|
||||
output = input
|
||||
|
|
Loading…
Reference in New Issue