Class: Hbserver::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/hbserver/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(_port, _env) ⇒ Server

Returns a new instance of Server.



6
7
8
# File 'lib/hbserver/server.rb', line 6

def initialize _port,_env
  @port,@env = _port,_env
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hbserver/server.rb', line 9

def call env
  request = Rack::Request.new env
  response = Rack::Response.new

  _path_info = request.path_info               #=> uri 路径
  _params = request.params                     #=> 参数
  _request_method = request.request_method     #=> 请求方法 get post xhr 等
                    

  # _body = env.map{|k,v| "#{k} => #{v}" } .join("<br/>")
  # _body = {a: 123, b: 456}.to_json
  _body = {
      ip:  request.ip,
      cookies: request.cookies,
      session: request.session,
      session_options: request.session_options,
      referer: request.referer,
      user_agent: request.user_agent,
      method: request.request_method,
      path: request.path,
      path_info: request.path_info,
      params: request.params,
      port: request.port,
      url: request.url,
      base_url: request.base_url,
      fullpath: request.fullpath,
      host: request.host,
      host_with_port: request.host_with_port,
      logger: request.logger,
      media_type: request.media_type,
      media_type_params: request.media_type_params,
      POST: request.POST,
      query_string: request.query_string,
      scheme: request.scheme,
      script_name: request.script_name,
      ssl: request.ssl?,
      trace: request.trace?,
      xhr: request.xhr?,
      client: request['client']
  }.to_json

  if _path_info == "/baidu"
    response.redirect('http://baidu.com')
  else
    # response.body = [_body]
    response.write _body
    response.headers["Content-Length"] = _body.to_s.bytesize.to_s

    response.headers['Transfer-Encoding'] = 'chunked'

    # response.headers["Content-Type"] = "text/html"
    response.headers["Content-Type"] = "application/json"
    # response.headers["Content-Type"] = "text/plain"

    response.status = 200
  end

  response.finish

  #[200,{},[_request_method]]
end

#startObject



71
72
73
# File 'lib/hbserver/server.rb', line 71

def start
  Rack::Handler::Thin.run self,:Port => @port
end