Skip to content

Commit 307ffef

Browse files
author
Greg Hill
committed
merge changes with upstream
2 parents e4f7f7a + 706ed34 commit 307ffef

5 files changed

Lines changed: 70 additions & 64 deletions

File tree

README

Lines changed: 0 additions & 48 deletions
This file was deleted.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# eventmachine_httpserver
2+
3+
## EM::HttpServer vs Thin
4+
5+
+careo | is there a 25 word version of how it differs from thin?
6+
+tmm1 | good question.. its probably faster, but only because it doesn't have a real http parser like thin
7+
+wyhaines | It is faster. It's more of an nginx style parser than the mongrel, grammar based parser.
8+
+careo | so perhaps a better fit for putting an http interface on top of a bunch of already-evented code?
9+
+wyhaines | careo: It depends. There are very valid arguments for using an RFC-compliant parser a lot of the time.
10+
+wyhaines | But, yeah, sometimes being strictly RFC compliant isn't something that you care about.
11+
12+
## Installing
13+
14+
### Without Bundler
15+
sudo gem install eventmachine_httpserver
16+
17+
### With bundler
18+
19+
gem 'eventmachine_httpserver', :require => 'evma_httpserver'
20+
21+
## Usage example
22+
require 'eventmachine'
23+
require 'evma_httpserver'
24+
25+
class MyHttpServer < EM::Connection
26+
include EM::HttpServer
27+
28+
def post_init
29+
super
30+
no_environment_strings
31+
end
32+
33+
def process_http_request
34+
# the http request details are available via the following instance variables:
35+
# @http_protocol
36+
# @http_request_method
37+
# @http_cookie
38+
# @http_if_none_match
39+
# @http_content_type
40+
# @http_path_info
41+
# @http_request_uri
42+
# @http_query_string
43+
# @http_post_content
44+
# @http_headers
45+
46+
response = EM::DelegatedHttpResponse.new(self)
47+
response.status = 200
48+
response.content_type 'text/html'
49+
response.content = '<center><h1>Hi there</h1></center>'
50+
response.send_response
51+
end
52+
end
53+
54+
EM.run{
55+
EM.start_server '0.0.0.0', 8080, MyHttpServer
56+
}

ext/extconf.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
flags << '-D OS_SOLARIS8'
6565
flags << '-D BUILD_FOR_RUBY'
6666

67-
dir_config('ssl')
67+
pkg_config('openssl') || dir_config('ssl')
6868
if have_library('ssl') and
6969
have_library('crypto') and
7070
have_header('openssl/ssl.h') and
@@ -81,7 +81,7 @@
8181
flags << '-DOS_UNIX'
8282
flags << '-DBUILD_FOR_RUBY'
8383

84-
dir_config('ssl')
84+
pkg_config('openssl') || dir_config('ssl')
8585
if have_library('ssl') and
8686
have_library('crypto') and
8787
have_library('C') and
@@ -103,7 +103,7 @@
103103
flags << '-DOS_UNIX'
104104
flags << '-DBUILD_FOR_RUBY'
105105

106-
dir_config('ssl')
106+
pkg_config('openssl') || dir_config('ssl')
107107
if have_library('ssl') and
108108
have_library('crypto') and
109109
have_header('openssl/ssl.h') and

ext/http.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ bool HttpConnection_t::_DetectVerbAndSetEnvString (const char *request, int verb
543543
"POST",
544544
"PUT",
545545
"DELETE",
546-
"HEAD"
546+
"HEAD",
547+
"OPTIONS"
547548
};
548549

549550
int n_verbs = sizeof(verbs) / sizeof(const char*);

lib/evma_httpserver/response.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,16 @@ class HttpResponse
9292
505 => "505 HTTP Version Not Supported"
9393
}
9494

95-
attr_accessor :status, :content, :headers, :chunks, :multiparts
95+
attr_accessor :status, :headers, :chunks, :multiparts
9696

9797
def initialize
9898
@headers = {}
9999
end
100100

101+
def content=(value) @content = value.to_s end
102+
def content() @content || '' end
103+
def content?() !!@content end
104+
101105
def keep_connection_open arg=true
102106
@keep_connection_open = arg
103107
end
@@ -189,7 +193,7 @@ def generate_header_lines in_hash
189193
#
190194
def fixup_headers
191195
if @content
192-
@headers["Content-Length"] = @content.to_s.length
196+
@headers["Content-Length"] = @content.bytesize
193197
elsif @chunks
194198
@headers["Transfer-Encoding"] = "chunked"
195199
# Might be nice to ENSURE there is no content-length header,
@@ -207,14 +211,11 @@ def fixup_headers
207211
# DO NOT close the connection or send any goodbye kisses. This method can
208212
# be called multiple times to send out chunks or multiparts.
209213
def send_body
210-
if @content
211-
send_content
212-
elsif @chunks
214+
if @chunks
213215
send_chunks
214216
elsif @multiparts
215217
send_multiparts
216218
else
217-
@content = ""
218219
send_content
219220
end
220221
end
@@ -225,9 +226,7 @@ def send_body
225226
#
226227
def send_trailer
227228
send_headers unless @sent_headers
228-
if @content
229-
# no-op
230-
elsif @chunks
229+
if @chunks
231230
unless @last_chunk_sent
232231
chunk ""
233232
send_chunks
@@ -238,15 +237,13 @@ def send_trailer
238237
# supposed to interact with the case where we leave the connection
239238
# open after transmitting the multipart response.
240239
send_data "\r\n--#{@multipart_boundary}--\r\n\r\n"
241-
else
242-
# no-op
243240
end
244241
end
245242

246243
def send_content
247244
raise "sent content already" if @sent_content
248245
@sent_content = true
249-
send_data((@content || "").to_s)
246+
send_data(content)
250247
end
251248

252249
# add a chunk to go to the output.

0 commit comments

Comments
 (0)