コード例 #1
0
ファイル: gateway.go プロジェクト: xiaojiaqi/fakewechat
func file(rw http.ResponseWriter, req *http.Request) {

	hj, ok := rw.(http.Hijacker)
	if !ok {
		http.Error(rw, "webserver doesn't support hijacking", http.StatusInternalServerError)
		return
	}
	netconn, bio, err := hj.Hijack()
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}
	url := req.URL
	fmt.Println(url.Path)
	b, _ := fileCache[url.Path]
	bio.WriteString("HTTP/1.1 200 OK\r\n")

	bio.WriteString("server: gateway\r\n")

	bio.WriteString(string("Content-Length: " + strconv.Itoa(len(b)) + "\r\n\r\n"))
	bio.Write(b)
	bio.Flush()

	netconn.Close()
}
コード例 #2
0
ファイル: gateway.go プロジェクト: xiaojiaqi/fakewechat
func sendtoQue(types int, rw http.ResponseWriter, req *http.Request) {
	hj, ok := rw.(http.Hijacker)
	if !ok {
		http.Error(rw, "webserver doesn't support hijacking", http.StatusInternalServerError)
		return
	}
	netconn, bio, err := hj.Hijack()
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	q := &HttpContext{
		response:    &rw,
		request:     req,
		requestType: types,
		conn:        netconn,
		bufferIO:    bio,
		//httpconn:     http.Conn(hj),
		//result: make(chan HttpContext),
	}
	s := atomic.AddUint32(&counter, 1)

	channelId := s % GateWayChannelSize
	if nil != pushtoQueue(&channel[channelId], q) {
		fmt.Println("too many request, close")
		bio.WriteString("HTTP/1.1 555 OK\r\n")
		bio.WriteString("server: gateway\r\n")
		bio.WriteString("Connection: close\r\n\r\n")
		bio.Flush()
		netconn.Close()
	}
}