├── .appveyor.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── Rakefile ├── TODO ├── build_config.rb ├── build_config.rb.lock ├── example ├── server.rb └── uds.rb ├── mrbgem.rake ├── mrblib └── mrb_simplehttpserver.rb └── test └── mrb_simplehttpserver.rb /.appveyor.yml: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2017 Sebastian Katzer 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | version: "{build}" 24 | 25 | environment: 26 | global: 27 | COMPILER: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1 28 | TOOLCHAIN: gcc 29 | matrix: 30 | - MRUBY_VERSION: 1.3.0 31 | - MRUBY_VERSION: 1.4.0 32 | - MRUBY_VERSION: head 33 | 34 | matrix: 35 | allow_failures: 36 | - MRUBY_VERSION: head 37 | 38 | init: 39 | - SET PATH=%COMPILER%\bin;C:\Ruby25-x64\bin;%PATH%;C:\cygwin64\bin 40 | - gcc --version 41 | 42 | build_script: 43 | - rake mruby 44 | - IF "%MRUBY_VERSION%"=="1.3.0" (DEL /F /Q mruby\build\mrbgems\mruby-io\test\) ELSE (DEL /F /Q mruby\mrbgems\mruby-io\test\) 45 | - rake compile 46 | 47 | test_script: 48 | - rake test 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /mruby 2 | /tmp -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) MATSUMOTO Ryosuke 2014 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | language: c 24 | 25 | compiler: 26 | - gcc 27 | - clang 28 | 29 | env: 30 | - MRUBY_VERSION=1.4.1 31 | - MRUBY_VERSION=head 32 | 33 | matrix: 34 | allow_failures: 35 | - env: MRUBY_VERSION=head 36 | 37 | before_script: 38 | - export TOOLCHAIN=$CC 39 | 40 | script: 41 | - rake compile 42 | - rake test 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | mruby-simplehttpserver 2 | 3 | Copyright (c) MATSUMOTO Ryosuke 2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | [ MIT license: http://www.opensource.org/licenses/mit-license.php ] 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mruby-simplehttpserver [](https://travis-ci.org/matsumotory/mruby-simplehttpserver) 2 | 3 | mruby-simplehttpserver is a HTTP Server with less dependency for mruby. mruby-simplehttpserver depends on mruby-io, mruby-socket and mruby-http. A Web server using mruby-simplehttpserver run on a environment which is not very rich like [OSv](http://osv.io/) or simple Linux box. 4 | 5 | ### Install by mrbgems 6 | 7 | add conf.gem line to `build_config.rb`: 8 | 9 | ```ruby 10 | MRuby::Build.new do |conf| 11 | 12 | # ... (snip) ... 13 | 14 | conf.gem mgem: 'mruby-simplehttpserver' 15 | end 16 | ``` 17 | 18 | ## How to use SimpleHttpServer 19 | 20 | SimpleHttpServer class provides a HTTP Server. 21 | 22 | SimpleHttpServer has a [Rack](http://rack.github.io/)-like interface, so you should provide an "app": an object that responds to `#call`, taking the environment hash as a parameter, and returning an Array with three elements: 23 | 24 | - HTTP Status Code 25 | - Headers hash 26 | - Body 27 | 28 | #### Example: a simple "OK" server 29 | 30 | The following example code can be used as the basis of a HTTP Server which returning "OK": 31 | 32 | ```ruby 33 | app = -> (env) { [200, { 'Content-Type' => 'text/plain' }, ['OK']] } 34 | 35 | server = SimpleHttpServer.new( 36 | host: 'localhost', 37 | port: 8000, 38 | app: app, 39 | ) 40 | 41 | server.run 42 | ``` 43 | 44 | `SimpleHttpServer#run` invokes a server that returns "OK". (If you want to stop the server, enter `^C` key.) You can see its response with curl: 45 | 46 | ```console 47 | $ curl localhost:8000 48 | OK 49 | ``` 50 | 51 | If you see more examples, see [example/server.rb](https://github.com/matsumoto-r/mruby-simplehttpserver/blob/master/example/server.rb). 52 | 53 | #### What does `env` receive? 54 | 55 | `env`, which an "app" takes as a parameter, receives a hash object includes request headers and the following parameters: 56 | 57 | - REQUEST\_METHOD ... GET, PUT, POST, DELETE and so on. 58 | - PATH\_INFO ... request path or '/' 59 | - QUERY\_STRING ... query string 60 | - HTTP\_VERSION ... 'HTTP/1.1' 61 | 62 | If you want to see how to parse an request, see also [mattn/mruby-http](https://github.com/mattn/mruby-http). 63 | 64 | Public Class Methods 65 | --- 66 | 67 | ### new(server\_ip = nil, port = nil, nonblock = nil, app = nil, debug = nil) 68 | 69 | Creates a new SimpleHttpServer object. 70 | 71 | The `:server_ip` should be a DNS hostname or IP address, the `:port` should be the listen port that server operates on. 72 | 73 | If the `:nonblock` is `true`, take non-blocking mode. When default (nonblock = nil), it behaves blocking-mode. 74 | 75 | The `:app` should be an object that responds to `#call`, taking the environment hash as a parameter, and returning an Array with three elements: 76 | 77 | - HTTP Status Code (Integer) 78 | - Headers hash (Hash) 79 | - Body (Array) 80 | 81 | it's like a [Rack](http://rack.github.io/) interface. 82 | 83 | If you want to debug, you can set `:debug` true. 84 | 85 | Public Instance Methods 86 | --- 87 | 88 | ### run() 89 | 90 | A process requests on sock. 91 | 92 | ## License 93 | 94 | under the MIT License: 95 | 96 | - see [LICENSE](./LICENSE) file 97 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) Sebastian Katzer 2017 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | ENV['MRUBY_CONFIG'] ||= File.expand_path('build_config.rb') 24 | ENV['MRUBY_VERSION'] ||= '2.1.2' 25 | 26 | file :mruby do 27 | if ENV['MRUBY_VERSION'] == 'head' 28 | sh 'git clone --depth 1 git://github.com/mruby/mruby.git' 29 | else 30 | sh "curl -L --fail --retry 3 --retry-delay 1 https://github.com/mruby/mruby/archive/#{ENV['MRUBY_VERSION']}.tar.gz -s -o - | tar zxf -" # rubocop:disable LineLength 31 | mv "mruby-#{ENV['MRUBY_VERSION']}", 'mruby' 32 | end 33 | end 34 | 35 | Rake::Task[:mruby].invoke 36 | 37 | namespace :mruby do 38 | Dir.chdir('mruby') { load 'Rakefile' } 39 | end 40 | 41 | task default: :compile 42 | 43 | desc 'compile binary' 44 | task compile: 'mruby:all' 45 | 46 | desc 'test' 47 | task test: 'mruby:test' 48 | 49 | desc 'cleanup' 50 | task clean: 'mruby:clean' 51 | 52 | desc 'cleanup all' 53 | task cleanall: 'mruby:deep_clean' 54 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | More example 2 | Test 3 | 4 | -------------------------------------------------------------------------------- /build_config.rb: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) MATSUMOTO Ryosuke 2014 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | MRuby::Build.new do |conf| 24 | toolchain ENV.fetch('TOOLCHAIN', :gcc) 25 | 26 | conf.enable_debug 27 | conf.enable_test 28 | 29 | conf.gem __dir__ 30 | conf.gembox :default 31 | end 32 | -------------------------------------------------------------------------------- /build_config.rb.lock: -------------------------------------------------------------------------------- 1 | --- 2 | mruby_version: 3 | version: 2.1.2 4 | release_no: 20102 5 | builds: 6 | host: 7 | https://github.com/mattn/mruby-http.git: 8 | url: https://github.com/mattn/mruby-http.git 9 | branch: HEAD 10 | commit: 003c2af0a059c50a3546001860108ff4bf3a2cac 11 | version: 0.0.0 12 | https://github.com/katzer/mruby-shelf.git: 13 | url: https://github.com/katzer/mruby-shelf.git 14 | branch: HEAD 15 | commit: 3bf2bc69eaceb4555ded37081e61c247808e6d88 16 | version: 0.0.0 17 | https://github.com/ksss/mruby-stringio.git: 18 | url: https://github.com/ksss/mruby-stringio.git 19 | branch: HEAD 20 | commit: 97cc4434519e85a0785a1b4cf6f872a220f772db 21 | version: 0.0.0 22 | https://github.com/katzer/mruby-process.git: 23 | url: https://github.com/katzer/mruby-process.git 24 | branch: HEAD 25 | commit: 03a4f8110b4d32b5ff56f78560c255e80dcf9219 26 | version: 0.0.0 27 | https://github.com/katzer/mruby-r3.git: 28 | url: https://github.com/katzer/mruby-r3.git 29 | branch: HEAD 30 | commit: c5b5ae647a8f04a86b4cd519273ba243bd459aec 31 | version: 0.0.0 32 | https://github.com/iij/mruby-env.git: 33 | url: https://github.com/iij/mruby-env.git 34 | branch: HEAD 35 | commit: 056ae324451ef16a50c7887e117f0ea30921b71b 36 | version: 0.0.0 37 | https://github.com/katzer/mruby-os.git: 38 | url: https://github.com/katzer/mruby-os.git 39 | branch: HEAD 40 | commit: 8e6e1dca8284f142bea681bfe032ebe7bae3851a 41 | version: 0.0.0 42 | -------------------------------------------------------------------------------- /example/server.rb: -------------------------------------------------------------------------------- 1 | app = Proc.new do |env| 2 | code = 200 3 | headers = { 'Server' => 'mruby-simplehttpserver' } 4 | path = env['PATH_INFO'] 5 | method = env['REQUEST_METHOD'] 6 | body = nil 7 | 8 | case path 9 | when '/mruby' 10 | body = "Hello mruby World.\n" 11 | when "/html" 12 | headers['Content-type'] = 'text/html; charset=utf-8' 13 | body = "