先輩にH2O良さ気と聞いたので気になって試してみた。 大量にググるより手を動かした方が良さそうですしね。

H2Oとは

DeNAの @kazuho さんが作られている高速なHTTPサーバー。
Nginxの2倍早いとか聞いて。
スーパークリエータ様恐るべし。

とりあえずインストール

今回の検証OSはCentOS6系です。

必要なものをinstall

sudo yum -y groupinstall "Development Tools"
sudo yum -y install curl curl-devel
sudo yum -y install libarchive libarchive-devel
sudo yum -y install expat expat-devel

# cmake
wget http://www.cmake.org/files/v3.0/cmake-3.0.2.tar.gz
tar -xvf cmake-3.0.2.tar.gz
cd cmake-3.0.2
./bootstrap --prefix=/usr       \
        --system-libs       \
        --mandir=/share/man \
        --docdir=/share/doc/cmake-3.0.2   &&
make
sudo make install

# libyaml
wget  http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz
tar -xvf yaml-0.1.5.tar.gz
cd yaml-0.1.5
./configure
make
sudo make install

続いてH2Oのインストール

git clone https://github.com/h2o/h2o.git
cd h2o/
cmake -DWITH_BUNDLED_SSL=on .
make
sudo make install

h2o -h
h2o version 1.2.1-alpha1

Usage:
  h2o [options]

Options:
  -c, --conf FILE    configuration file (default: h2o.conf)
  -m, --mode <mode>  specifies one of the following mode
                     - worker: invoked process handles incoming connections
                               (default)
                     - daemon: spawns a master process and exits. `error-log`
                               must be configured when using this mode, as all
                               the errors are logged to the file instead of
                               being emitted to STDERR
                     - master: invoked process becomes a master process (using
                               the `share/h2o/start_server` command) and spawns
                               a worker process for handling incoming
                               connections. Users may send SIGHUP to the master
                               process to reconfigure or upgrade the server.
                     - test:   tests the configuration and exits
  -t, --test         synonym of `--mode=test`
  -v, --version      prints the version number
  -h, --help         print this help

Please refer to the documentation under `share/doc/h2o` (or available online at
http://h2o.github.io/) for how to configure the server.

設定ファイル

yamlでかける。読みやすくて素敵。
xmlで設定書くサーバーなんてね。うん。。いや, なにも言うまい。

とりあえず簡単な設定で。 簡単なEcho WebSocket Serverの前にH20立ててみます。

listen:
  port: 8080
user: nobody
default:
    paths:
      /:
        proxy.reverse.url: "http://127.0.0.1:9090/ws"
access-log: /dev/stdout
error-log: /dev/stdout

お試し用のWebSocketEcho ServerはPythonで。

import tornado.ioloop
import tornado.web
import tornado.websocket

class WSEchoHandler(tornado.websocket.WebSocketHandler):
  def open(self):
    print ('[onopen]connection opened...')
    self.write_message("Connection was accepted.")

  def on_message(self, message):
    print('[onmessage]"received:', message)
    self.write_message(message)

  def on_close(self):
    print('[onclose]connection closed...')

application = tornado.web.Application([
  (r'/ws', WSEchoHandler)
])

if __name__ == "__main__":
  application.listen(9090)
  tornado.ioloop.IOLoop.instance().start()

H2Oを前に立てて接続を試してみると

--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8080
Origin: http://localhost:8080
Sec-WebSocket-Key: jr94pSavSo6zv1OPbBxpmQ==
Sec-WebSocket-Version: 13


-----------------------
--- response header ---
HTTP/1.1 400 Bad Request
Date: Mon, 11 May 2015 00:03:01 GMT
Server: h2o/1.2.1-alpha1
Connection: close
Content-Length: 34
content-type: text/html; charset=UTF-8
upgrade: websocket
-----------------------
できない。BadRequest。
直接PythonへWebsocketは接続できる。
レスポンスHeaderのConnectionがUpgradeになっていないので。
設定ファイルに書いてみる。
listen:
  port: 8080
user: nobody
default:
    paths:
      /:
        header.append: "Connection: Upgrade"
        proxy.reverse.url: "http://127.0.0.1:9090/ws"
access-log: /dev/stdout
error-log: /dev/stdout

H2Oを実行すると

[setting.yml:9] in command header.append, the named header cannot be rewritten
書き換えはダメなんだそうです。
う〜ん。 分からない。。。
今回は一旦保留で・・・。
原因解明次第更新します。

追記 (2015/05/18)

WebSocketでの使用は今のところできないようです。
公式ドキュメントに以下の記載。
websocket is only usable at the library level
Githubに issue書いた
公式に書いてあって申し訳無い感。。。
当面WebSocketで使うときはNginxですかね。

Related Posts