Optimize PAC generation.

This commit is contained in:
Chen Yufei
2012-12-08 22:37:24 +08:00
parent 93edb2fc18
commit 2908c9d02b
2 changed files with 17 additions and 17 deletions

32
pac.go
View File

@@ -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())
}

View File

@@ -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) {