diff --git a/pac.go b/pac.go index d5a45ac..3373402 100644 --- a/pac.go +++ b/pac.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "fmt" "io" "os" @@ -9,8 +8,6 @@ import ( "text/template" ) -var pacDirect = []byte("function FindProxyForURL(url, host) {return 'DIRECT'; };") - const pacRawTmpl = `var direct = 'DIRECT'; var httpProxy = '{{.ProxyAddr}}'; @@ -90,6 +87,11 @@ func initProxyServerAddr() { } } +// No need for content-length as we are closing connection +var pacHeader = []byte("HTTP/1.1 200 OK\r\nServer: cow-proxy\r\n" + + "Content-Type: application/x-ns-proxy-autoconfig\r\nConnection: close\r\n\r\n") +var pacDirect = []byte("function FindProxyForURL(url, host) { return 'DIRECT'; };") + func sendPAC(w io.Writer) { // domains in PAC file needs double quote ds1 := strings.Join(alwaysDirectDs.toArray(), "\",\n\"") @@ -103,9 +105,10 @@ func sendPAC(w io.Writer) { ds = ds1 + "\",\n\"" + ds2 } if ds == "" { - return pacDirect - } else { - ds = ",\n\"" + ds + "\"" + // Empty direct domain list + w.Write(pacHeader) + w.Write(pacDirect) + return } data := struct { @@ -113,19 +116,16 @@ func sendPAC(w io.Writer) { DirectDomains string }{ proxyServerAddr, - ds, + ",\n\"" + ds + "\"", } + if _, err := w.Write(pacHeader); err != nil { + debug.Println("Error writing pac header") + return + } // debug.Println("direct:", data.DirectDomains) - - buf := new(bytes.Buffer) - if err := pacTmpl.Execute(buf, data); err != nil { + if err := pacTmpl.Execute(w, data); err != nil { errl.Println("Error generating pac file:", err) - os.Exit(1) + return } - header := new(bytes.Buffer) - header.WriteString("HTTP/1.1 200 Okay\r\nServer: cow-proxy\r\nContent-Type: text/html\r\nConnection: close\r\n") - header.WriteString(fmt.Sprintf("Content-Length: %d\r\n\r\n", buf.Len())) - w.Write(header.Bytes()) - w.Write(buf.Bytes()) } diff --git a/proxy.go b/proxy.go index 2559b3d..bbad874 100644 --- a/proxy.go +++ b/proxy.go @@ -725,7 +725,7 @@ func (h *Handler) setStateResponsReceived(host string) { } } -var connEstablished = []byte("HTTP/1.0 200 Connection established\r\nProxy-agent: cow-proxy/0.1\r\n\r\n") +var connEstablished = []byte("HTTP/1.0 200 Connection established\r\nProxy-agent: cow-proxy\r\n\r\n") // Do HTTP CONNECT func (h *Handler) doConnect(r *Request, c *clientConn) (err error) {