Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #33 from vanne/support-https
Browse files Browse the repository at this point in the history
Support for https
  • Loading branch information
ryanb committed Feb 14, 2012
2 parents 910faa6 + ceaa331 commit 914871a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ rackup private_pub.ru -s thin -E production

It's not necessary to include faye.js since that will be handled automatically for you.

## Serving Faye over HTTPS (with Thin)

To server Faye over HTTPS you could create a thin configuration file `config/private_pub_thin.yml` similar to the following:

```yaml
---
port: 4443
ssl: true
ssl_key_file: /path/to/server.pem
ssl_cert_file: /path/to/certificate_chain.pem
environment: production
rackup: private_pub.ru
```
The `certificate_chain.pem` file should contain your signed certificate, followed by intermediate certificates (if any) and the root certificate of the CA that signed the key.

Next reconfigure the URL in `config/private_pub.yml` to look like `https://your.hostname.com:4443/faye`

Finally start up Thin from the project root.

```
thin -C config/private_pub_thin.yml start
```
## Usage
Expand Down
9 changes: 8 additions & 1 deletion lib/private_pub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ def publish_to(channel, data)
# Sends the given message hash to the Faye server using Net::HTTP.
def publish_message(message)
raise Error, "No server specified, ensure private_pub.yml was loaded properly." unless config[:server]
Net::HTTP.post_form(URI.parse(config[:server]), :message => message.to_json)
url = URI.parse(config[:server])

form = Net::HTTP::Post.new(url.path.empty? ? '/' : url.path)
form.set_form_data(:message => message.to_json)

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = url.scheme == "https"
http.start {|h| h.request(form)}
end

# Returns a message hash for sending to Faye
Expand Down
33 changes: 31 additions & 2 deletions spec/private_pub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,40 @@

it "publish message as json to server using Net::HTTP" do
PrivatePub.config[:server] = "http://localhost"
message = stub(:to_json => "message_json")
Net::HTTP.should_receive(:post_form).with(URI.parse("http://localhost"), :message => "message_json").and_return(:result)
message = 'foo'
form = mock(:post).as_null_object
http = mock(:http).as_null_object

Net::HTTP::Post.should_receive(:new).with('/').and_return(form)
form.should_receive(:set_form_data).with(message: 'foo'.to_json)

Net::HTTP.should_receive(:new).with('localhost', 80).and_return(http)
http.should_receive(:start).and_yield(http)
http.should_receive(:request).with(form).and_return(:result)

PrivatePub.publish_message(message).should eq(:result)
end

it "it should use HTTPS if the server URL says so" do
PrivatePub.config[:server] = "https://localhost"
http = mock(:http).as_null_object

Net::HTTP.should_receive(:new).and_return(http)
http.should_receive(:use_ssl=).with(true)

PrivatePub.publish_message('foo')
end

it "it should not use HTTPS if the server URL says not to" do
PrivatePub.config[:server] = "http://localhost"
http = mock(:http).as_null_object

Net::HTTP.should_receive(:new).and_return(http)
http.should_receive(:use_ssl=).with(false)

PrivatePub.publish_message('foo')
end

it "raises an exception if no server is specified when calling publish_message" do
lambda {
PrivatePub.publish_message("foo")
Expand Down

0 comments on commit 914871a

Please sign in to comment.