package main
import (
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
}
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
return
}
conn, _, _ := hj.Hijack()
conn.Close()
})
http.ListenAndServe(":8080", nil)
}
Here's what it looks like via curl (after running "go run testserver.go")
~ ᐅ curl -v http://localhost:8080/hijack * About to connect() to localhost port 8080 (#0) * Trying 127.0.0.1... * connected * Connected to localhost (127.0.0.1) port 8080 (#0) > GET /hijack HTTP/1.1 > User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5 > Host: localhost:8080 > Accept: */* > * Empty reply from server * Connection #0 to host localhost left intact curl: (52) Empty reply from server * Closing connection #0
Now let's see what NSURLConnection's sendAsynchronousRequest completion handler reports:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/hijack"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"%d", [(NSHTTPURLResponse *)response statusCode]);
NSLog(@"%@", error);
}];
--
2013-05-09 14:41:00.221 testserver[59315:c07] 200
2013-05-09 14:41:00.222 testserver[59315:c07] (null)
There you have it! Connection closed results in a 200 http response code and no error!