Skip to content

Use HTTPS where necessary in HTTP.Client #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"purescript-maps": "^0.5.4",
"purescript-options": "^0.6.0",
"purescript-unsafe-coerce": "^0.1.0",
"purescript-node-streams": "^0.3.0"
"purescript-node-streams": "^0.3.0",
"purescript-node-url": "^0.1.1"
}
}
4 changes: 3 additions & 1 deletion src/Node/HTTP/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// module Node.HTTP.Client

var http = require('http');
var https = require('https');

exports.requestImpl = function(opts) {
return function(k) {
return function() {
return http.request(opts, function(res) {
var lib = opts.protocol === 'https:' ? https : http;
return lib.request(opts, function(res) {
k(res)();
});
};
Expand Down
3 changes: 2 additions & 1 deletion src/Node/HTTP/Client.purs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Data.StrMap (StrMap())

import Node.HTTP (HTTP())
import Node.Stream (Readable, Writable)
import Node.URL as URL

import Control.Monad.Eff (Eff)

Expand Down Expand Up @@ -84,7 +85,7 @@ request = requestImpl <<< options

-- | Make a HTTP request from a URI string and response callback.
requestFromURI :: forall eff. String -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request
requestFromURI = requestImpl <<< toForeign
requestFromURI = requestImpl <<< toForeign <<< URL.parse

-- | Create a writable stream from a request object.
requestAsStream :: forall eff r. Request -> Writable r (http :: HTTP | eff)
Expand Down
21 changes: 16 additions & 5 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import Node.Encoding
foreign import stdout :: forall eff r. Writable r eff

main = do
testBasic
testHttps

testBasic = do
server <- createServer respond
listen server 8080 $ void do
log "Listening on port 8080."
req <- Client.requestFromURI "http://localhost:8080/" \response -> void do
log "Response from GET /:"
let responseStream = Client.responseAsStream response
pipe responseStream stdout
end (Client.requestAsStream req) (return unit)
simpleReq "http://localhost:8080"
where
respond req res = do
setStatusCode res 200
Expand All @@ -41,3 +41,14 @@ main = do
writeString outputStream UTF8 html(return unit)
end outputStream (return unit)
"POST" -> void $ pipe inputStream outputStream

testHttps =
simpleReq "https://api.github.com"

simpleReq uri = do
log ("GET " <> uri <> ":")
req <- Client.requestFromURI uri \response -> void do
log "Response:"
let responseStream = Client.responseAsStream response
pipe responseStream stdout
end (Client.requestAsStream req) (return unit)