From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/sample/webrick/demo-app.rb | 66 +++++++++++++++++++++++++++++ jni/ruby/sample/webrick/demo-multipart.cgi | 12 ++++++ jni/ruby/sample/webrick/demo-servlet.rb | 6 +++ jni/ruby/sample/webrick/demo-urlencoded.cgi | 12 ++++++ jni/ruby/sample/webrick/hello.cgi | 11 +++++ jni/ruby/sample/webrick/hello.rb | 8 ++++ jni/ruby/sample/webrick/httpd.rb | 23 ++++++++++ jni/ruby/sample/webrick/httpproxy.rb | 25 +++++++++++ jni/ruby/sample/webrick/httpsd.rb | 33 +++++++++++++++ 9 files changed, 196 insertions(+) create mode 100644 jni/ruby/sample/webrick/demo-app.rb create mode 100644 jni/ruby/sample/webrick/demo-multipart.cgi create mode 100644 jni/ruby/sample/webrick/demo-servlet.rb create mode 100644 jni/ruby/sample/webrick/demo-urlencoded.cgi create mode 100644 jni/ruby/sample/webrick/hello.cgi create mode 100644 jni/ruby/sample/webrick/hello.rb create mode 100644 jni/ruby/sample/webrick/httpd.rb create mode 100644 jni/ruby/sample/webrick/httpproxy.rb create mode 100644 jni/ruby/sample/webrick/httpsd.rb (limited to 'jni/ruby/sample/webrick') diff --git a/jni/ruby/sample/webrick/demo-app.rb b/jni/ruby/sample/webrick/demo-app.rb new file mode 100644 index 0000000..c7a2a0a --- /dev/null +++ b/jni/ruby/sample/webrick/demo-app.rb @@ -0,0 +1,66 @@ +require "pp" + +module DemoApplication + def initialize(config, enctype) + super + @enctype = enctype + end + + def do_GET(req, res) + if req.path_info != "/" + res.set_redirect(WEBrick::HTTPStatus::Found, req.script_name + "/") + end + res.body =<<-_end_of_html_ + +
+ text:
+ file:
+ check: + a, + b, + c, +
+ +
+ + _end_of_html_ + res['content-type'] = 'text/html; charset=iso-8859-1' + end + + def do_POST(req, res) + if req["content-length"].to_i > 1024*10 + raise WEBrick::HTTPStatus::Forbidden, "file size too large" + end + res.body =<<-_end_of_html_ + +

Query Parameters

+ #{display_query(req.query)} + return +

Request

+
#{WEBrick::HTMLUtils::escape(PP::pp(req, "", 80))}
+

Response

+
#{WEBrick::HTMLUtils::escape(PP::pp(res, "", 80))}
+ + _end_of_html_ + res['content-type'] = 'text/html; charset=iso-8859-1' + end + + private + + def display_query(q) + ret = "" + q.each{|key, val| + ret << "

#{WEBrick::HTMLUtils::escape(key)}

" + ret << "" + ret << make_tr("val", val.inspect) + ret << make_tr("val.to_a", val.to_a.inspect) + ret << make_tr("val.to_ary", val.to_ary.inspect) + ret << "
" + } + ret + end + + def make_tr(arg0, arg1) + "#{arg0}#{WEBrick::HTMLUtils::escape(arg1)}" + end +end diff --git a/jni/ruby/sample/webrick/demo-multipart.cgi b/jni/ruby/sample/webrick/demo-multipart.cgi new file mode 100644 index 0000000..0893fad --- /dev/null +++ b/jni/ruby/sample/webrick/demo-multipart.cgi @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby +require "webrick/cgi" +require "webrick/https" # should load if it runs on HTTPS server +require "./demo-app" + +class DemoCGI < WEBrick::CGI + include DemoApplication +end + +config = { :NPH => false } +cgi = DemoCGI.new(config, "multipart/form-data") +cgi.start diff --git a/jni/ruby/sample/webrick/demo-servlet.rb b/jni/ruby/sample/webrick/demo-servlet.rb new file mode 100644 index 0000000..9c18cc6 --- /dev/null +++ b/jni/ruby/sample/webrick/demo-servlet.rb @@ -0,0 +1,6 @@ +require "webrick" +require "./demo-app" + +class DemoServlet < WEBrick::HTTPServlet::AbstractServlet + include DemoApplication +end diff --git a/jni/ruby/sample/webrick/demo-urlencoded.cgi b/jni/ruby/sample/webrick/demo-urlencoded.cgi new file mode 100644 index 0000000..e4706f8 --- /dev/null +++ b/jni/ruby/sample/webrick/demo-urlencoded.cgi @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby +require "webrick/cgi" +require "webrick/https" # should load if it runs on HTTPS server +require "./demo-app" + +class DemoCGI < WEBrick::CGI + include DemoApplication +end + +config = { :NPH => false } +cgi = DemoCGI.new(config, "application/x-www-form-urlencoded") +cgi.start diff --git a/jni/ruby/sample/webrick/hello.cgi b/jni/ruby/sample/webrick/hello.cgi new file mode 100644 index 0000000..35d2240 --- /dev/null +++ b/jni/ruby/sample/webrick/hello.cgi @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +require "webrick/cgi" + +class HelloCGI < WEBrick::CGI + def do_GET(req, res) + res["content-type"] = "text/plain" + res.body = "Hello, world.\n" + end +end + +HelloCGI.new.start diff --git a/jni/ruby/sample/webrick/hello.rb b/jni/ruby/sample/webrick/hello.rb new file mode 100644 index 0000000..4d02676 --- /dev/null +++ b/jni/ruby/sample/webrick/hello.rb @@ -0,0 +1,8 @@ +require "webrick" + +class HelloServlet < WEBrick::HTTPServlet::AbstractServlet + def do_GET(req, res) + res["content-type"] = "text/plain" + res.body = "Hello, world.\n" + end +end diff --git a/jni/ruby/sample/webrick/httpd.rb b/jni/ruby/sample/webrick/httpd.rb new file mode 100644 index 0000000..b0edf47 --- /dev/null +++ b/jni/ruby/sample/webrick/httpd.rb @@ -0,0 +1,23 @@ +require "webrick" + +httpd = WEBrick::HTTPServer.new( + :DocumentRoot => File::dirname(__FILE__), + :Port => 10080, + :Logger => WEBrick::Log.new($stderr, WEBrick::Log::DEBUG), + :AccessLog => [ + [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ], + [ $stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT ], + [ $stderr, WEBrick::AccessLog::AGENT_LOG_FORMAT ], + ], + :CGIPathEnv => ENV["PATH"] # PATH environment variable for CGI. +) + +require "./hello" +httpd.mount("/hello", HelloServlet) + +require "./demo-servlet" +httpd.mount("/urlencoded", DemoServlet, "application/x-www-form-urlencoded") +httpd.mount("/multipart", DemoServlet, "multipart/form-data") + +trap(:INT){ httpd.shutdown } +httpd.start diff --git a/jni/ruby/sample/webrick/httpproxy.rb b/jni/ruby/sample/webrick/httpproxy.rb new file mode 100644 index 0000000..c84457e --- /dev/null +++ b/jni/ruby/sample/webrick/httpproxy.rb @@ -0,0 +1,25 @@ +require "webrick" +require "webrick/httpproxy" + +# The :ProxyContentHandler proc will be invoked before sending a response to +# the User-Agent. You can inspect the pair of request and response messages +# (or edit the response message if necessary). + +pch = Proc.new{|req, res| + p [ req.request_line, res.status_line ] +} + +def upstream_proxy + if prx = ENV["http_proxy"] + return URI.parse(prx) + end + return nil +end + +httpd = WEBrick::HTTPProxyServer.new( + :Port => 10080, + :ProxyContentHandler => pch, + :ProxyURI => upstream_proxy +) +Signal.trap(:INT){ httpd.shutdown } +httpd.start diff --git a/jni/ruby/sample/webrick/httpsd.rb b/jni/ruby/sample/webrick/httpsd.rb new file mode 100644 index 0000000..a120782 --- /dev/null +++ b/jni/ruby/sample/webrick/httpsd.rb @@ -0,0 +1,33 @@ +require "webrick" +require "webrick/https" + +hostname = WEBrick::Utils::getservername +subject = [["O", "ruby-lang.org"], ["OU", "sample"], ["CN", hostname]] +comment = "Comment for self-signed certificate" + +httpd = WEBrick::HTTPServer.new( + :DocumentRoot => File::dirname(__FILE__), + :Port => 10443, + :SSLEnable => true, + + # Specify key pair and server certificate. + # :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read("server.key")), + # :SSLCertificate => OpenSSL::X509::Certificate.new(File.read("server.crt")), + + # specify the following SSL options if you want to use auto + # generated self-signed certificate. + :SSLCertName => subject, + :SSLComment => comment, + + :CGIPathEnv => ENV["PATH"] # PATH environment variable for CGI. +) + +require "./hello" +httpd.mount("/hello", HelloServlet) + +require "./demo-servlet" +httpd.mount("/urlencoded", DemoServlet, "application/x-www-form-urlencoded") +httpd.mount("/multipart", DemoServlet, "multipart/form-data") + +trap(:INT){ httpd.shutdown } +httpd.start -- cgit v1.2.3-70-g09d2