示例#1
0
文件: main.go 项目: davecheney/poxy
func ProxyServer(w http.ResponseWriter, req *http.Request) {
	log.Printf("%#v", req)
	addr, err := net.ResolveIPAddr(req.URL.Host)
	if err != nil {
		io.WriteString(w, err.String())
		return
	}
	conn, err := net.DialTCP("tcp4", nil, &net.TCPAddr{IP: addr.IP, Port: 80})
	if err != nil {
		io.WriteString(w, err.String())
		return
	}
	defer conn.Close()
	client := http.NewClientConn(conn, nil)
	err = client.Write(req)
	if err != nil {
		io.WriteString(w, err.String())
		return
	}
	resp, err := client.Read()
	if err != nil {
		io.WriteString(w, err.String())
		return
	}
	for k, v := range resp.Header {
		w.SetHeader(k, v)
	}
	io.Copy(w, resp.Body)
	w.Flush()
}
示例#2
0
func Images(w http.ResponseWriter, r *http.Request) {

	imagePath := path.Base(r.URL.Path)
	mimeType := mime.TypeByExtension(path.Ext(imagePath))

	w.SetHeader("Content-Type", mimeType)
	w.SetHeader("Cache-Control", "max-age=31104000, public")
	current := View.Themes.Current()
	if current == nil {
		se := &ServerError{404, "Not Found"}
		se.Write(w)
		return
	}
	for _, v := range View.Resources {
		if v.Template == current.ID {
			if v.Name == imagePath {
				w.Write(v.Data)
				w.Flush()
				return
			}
		}
	}
	se := &ServerError{404, "Not Found"}
	se.Write(w)
	return
}
示例#3
0
func GlobalController(w http.ResponseWriter, r *http.Request) {
	imagePath := path.Base(r.URL.Path)
	mimeType := mime.TypeByExtension(path.Ext(imagePath))
	w.SetHeader("Content-Type", mimeType)
	for _, v := range View.Globals {
		if v.Name == imagePath {
			w.Write(v.Data)
			w.Flush()
		}
	}
}
示例#4
0
func (se *ServerError) Write(w http.ResponseWriter) {
	w.WriteHeader(se.Code)
	errOut := fmt.Sprintf(errHtml, se.Code, se.Msg)
	w.Write([]byte(errOut))
	w.Flush()
}