Class: Wildnet::Server::HttpHandler

Inherits:
SimpleChannelUpstreamHandler
  • Object
show all
Defined in:
lib/wildnet-server/http_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app.



17
18
19
# File 'lib/wildnet-server/http_handler.rb', line 17

def app
  @app
end

Instance Method Details

#handleObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/wildnet-server/http_handler.rb', line 103

def handle

  public_file = File.join(Rails.root, "public", @request["PATH_INFO"])
  if File.exists?(public_file) and not File.directory?(public_file)
    respond [200, {"X-Wildnet-Send-File" => public_file, "Content-Type" => "wildnet/detect"}, nil]
  else
    respond @app.call(@request)
  end

end

#message_received(context, event) ⇒ Object Also known as: messageReceived



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wildnet-server/http_handler.rb', line 19

def message_received context, event

  unless event.message.kind_of?(HttpRequest)

    chunk = event.message

    chunk.content.readBytes(@upload_stream, chunk.content.capacity)

    if chunk.isLast
      @upload_stream.close
      handle
    end

    return
  end

  request                       = event.message
  @request                      = Request.new

  @request["REQUEST_METHOD"]    = request.getMethod.getName
  @request["SCRIPT_NAME"]       = ""
  @request["PATH_INFO"]         = request.getUri
  @request["QUERY_STRING"]      = ""
  @request["SERVER_NAME"]       = HttpHeaders.getHost request, "localhost"
  @request["SERVER_PORT"]       = 80

  @request["rack.url_scheme"]   = "http"
  @request["rack.input"]        = File.new(java.io.File.createTempFile("wildnet.", ".request", java.io.File.new(File.join(Rails.root, "tmp"))).getAbsolutePath())
  @request["rack.errors"]       = $stderr
  @request["rack.multithread"]  = true
  @request["rack.multiprocess"] = false
  @request["rack.run_once"]     = false

  @upload_stream                = FileOutputStream.new(java.io.File.new(@request["rack.input"].path))
  unless request.isChunked
    request.content.readBytes(@upload_stream, request.getContent.capacity)
    @upload_stream.close
  end

  if request.containsHeader "X-For-Host"
    @request["SERVER_NAME"] = request.getHeader "X-For-Host"
  end

  if request.containsHeader "X-For-Scheme"
    @request["rack.url_scheme"] = request.getHeader "X-For-Scheme"
  end

  if server_name = %r{(.+):([0-9]+)}.match(@request["SERVER_NAME"])
    @request["SERVER_NAME"] = server_name[1]
    @request["SERVER_PORT"] = server_name[2].to_i
  end

  if path_info = %r{(.+)\?(.+)}.match(@request["PATH_INFO"])
    @request["PATH_INFO"]    = path_info[1]
    @request["QUERY_STRING"] = path_info[2]
  end

  if (@request["SERVER_PORT"] != 80 and @request["rack.url_scheme"] != "http") and (@request["SERVER_PORT"] != 443 and @request["rack.url_scheme"] != "https")
    request.removeHeader "Host"
    request.addHeader "Host", @request["SERVER_NAME"] + ":" + @request["SERVER_PORT"]
  else
    request.removeHeader "Host"
    request.addHeader "Host", @request["SERVER_NAME"]
  end

  request.getHeaderNames.each do |header|
    name = header.gsub("-", "_").upcase
    if (name != "CONTENT_TYPE" and name != "CONTENT_LENGTH")
      name = "HTTP_#{name}"
    end
    @request[name] = request.getHeader(header)
  end

  @request["rack.version"] = ::Rack::VERSION

  @request["wildnet.context"] = context
  @request["wildnet.response"] = HttpResponse.new(@request["wildnet.context"])

  unless request.isChunked
    handle
  end

end

#respond(response) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/wildnet-server/http_handler.rb', line 114

def respond response

  status, headers, body = response

  @request["wildnet.response"].status = status

  complete = headers.delete("X-Wildnet-Complete")
  send_file = headers.delete("X-Wildnet-Send-File")

  headers.each_pair do |header, value|
    @request["wildnet.response"][header] = value
  end

  if send_file
    @request["wildnet.response"].send_file(send_file)
  else
    body.each do |line|
      @request["wildnet.response"] << line
    end
    @request["wildnet.response"].flush
  end

  @request["wildnet.response"].close(complete)

  @request["rack.input"].close
  File.delete(@request["rack.input"].path)

end