当前位置: 首页>>代码示例>>Golang>>正文


Golang ResponseWriter.Flush方法代码示例

本文整理汇总了Golang中net/http.ResponseWriter.Flush方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseWriter.Flush方法的具体用法?Golang ResponseWriter.Flush怎么用?Golang ResponseWriter.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http.ResponseWriter的用法示例。


在下文中一共展示了ResponseWriter.Flush方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: httpProxy

func httpProxy(writer http.ResponseWriter, request *http.Request) {

	proxyRequest := new(http.Request)
	*proxyRequest = *request

	log.Printf("request = %s %s", request.Method, request.URL.Host)

	if strings.ToUpper(proxyRequest.Method) == "CONNECT" {
		hostPort := request.URL.Host
		pandora, err := dialer.Dial("tcp", hostPort) // tuner.pandora.com:443
		if err != nil {
			log.Printf("pianobarproxy: error: %v", err)
			writer.WriteHeader(http.StatusInternalServerError)
			return
		}

		client, writer, err := writer.(http.Hijacker).Hijack()
		writer.WriteString("HTTP/1.0 200 Connection Established\r\n\r\n")
		writer.Flush()
		go pipe(client, pandora)
		go pipe(pandora, client)
		return
	}
	proxyRequest.Proto = "HTTP/1.1"
	proxyRequest.ProtoMajor = 1
	proxyRequest.ProtoMinor = 1
	proxyRequest.Close = false

	// Remove the connection header to the backend.  We want a
	// persistent connection, regardless of what the client sent
	// to us.
	if proxyRequest.Header.Get("Connection") != "" {
		proxyRequest.Header = make(http.Header)
		copyHeader(proxyRequest.Header, request.Header)
		proxyRequest.Header.Del("Connection")
	}

	response, err := transport.RoundTrip(proxyRequest)
	if err != nil {
		log.Printf("pianobarproxy: error: %v", err)
		writer.WriteHeader(http.StatusInternalServerError)
		return
	}

	copyHeader(writer.Header(), response.Header)

	writer.WriteHeader(response.StatusCode)

	if response.Body != nil {
		io.Copy(io.Writer(writer), response.Body)
	}
}
开发者ID:robertkrimen,项目名称:pianobarproxy,代码行数:52,代码来源:main.go

示例2: serveQuery


//.........这里部分代码省略.........
				case <-notify:
					close(closing)
				}
			}()
		} else {
			defer close(closing)
		}
	}

	// Execute query.
	rw.Header().Add("Connection", "close")
	results := h.QueryExecutor.ExecuteQuery(query, influxql.ExecutionOptions{
		Database:  db,
		ChunkSize: chunkSize,
		ReadOnly:  r.Method == "GET",
		NodeID:    nodeID,
	}, closing)

	// If we are running in async mode, open a goroutine to drain the results
	// and return with a StatusNoContent.
	if async {
		go h.async(query, results)
		h.writeHeader(w, http.StatusNoContent)
		return
	}

	// if we're not chunking, this will be the in memory buffer for all results before sending to client
	resp := Response{Results: make([]*influxql.Result, 0)}

	// Status header is OK once this point is reached.
	// Attempt to flush the header immediately so the client gets the header information
	// and knows the query was accepted.
	h.writeHeader(rw, http.StatusOK)
	if w, ok := w.(http.Flusher); ok {
		w.Flush()
	}

	// pull all results from the channel
	rows := 0
	for r := range results {
		// Ignore nil results.
		if r == nil {
			continue
		}

		// if requested, convert result timestamps to epoch
		if epoch != "" {
			convertToEpoch(r, epoch)
		}

		// Write out result immediately if chunked.
		if chunked {
			n, _ := rw.WriteResponse(Response{
				Results: []*influxql.Result{r},
			})
			atomic.AddInt64(&h.stats.QueryRequestBytesTransmitted, int64(n))
			w.(http.Flusher).Flush()
			continue
		}

		// Limit the number of rows that can be returned in a non-chunked response.
		// This is to prevent the server from going OOM when returning a large response.
		// If you want to return more than the default chunk size, then use chunking
		// to process multiple blobs.
		rows += len(r.Series)
		if h.Config.MaxRowLimit > 0 && rows > h.Config.MaxRowLimit {
开发者ID:influxdata,项目名称:kapacitor,代码行数:67,代码来源:handler.go

示例3: handler

func handler(w http.ResponseWriter, req *http.Request) {
	defer func() {
		r := recover()
		if r != nil {
			w.Header().Set("Content-Type", "text/plain")
			Fprintf(w, "%v", r)
		}
	}()

	req.ParseForm()
	if f4, ok4 := req.Form["list"]; ok4 {
		what := f4[0]
		if what == "" {
			// List all files
			names, err := filepath.Glob("*.bas")
			if err != nil {
				panic(err)
			}
			sort.Strings(names)
			w.Header().Set("Content-Type", "text/html")
			Fprintf(w, "<html><body>")
			for _, name := range names {
				Fprintf(w, `<a href="/?list=%s">%s</a><br>`+"\n", name, name)
			}
		} else {
			// List one file
			fd, err := os.Open(what)
			if err != nil {
				panic(err)
			}
			w.Header().Set("Content-Type", "text/plain")
			_, err = io.Copy(w, fd)
			if err != nil {
				panic(err)
			}
			err = fd.Close()
			if err != nil {
				panic(err)
			}
		}
	} else if f1, ok1 := req.Form["run"]; ok1 {
		var putchar func(ch byte)
		forward_putchar := func(ch byte) {
			putchar(ch)
		}

		terp := NewTerp(f1[0], forward_putchar)
		terp.SetExpiration("30s")
		d := draw.Register(terp)
		putchar = d.Putchar

		if f3, ok3 := req.Form["progname"]; ok3 {
			name := f3[0]
			if name == "" {
				name = "untitled"
			}
			name = strings.Trim(name, " \t\r\n")
			flags := os.O_CREATE | os.O_WRONLY | os.O_APPEND
			if strings.HasSuffix(name, "!") {
				flags |= os.O_EXCL
			}
			fd, err := os.OpenFile(CurlyEncode(name)+".bas", flags, 0666)
			if err != nil {
				panic(err)
			}
			w := bufio.NewWriter(fd)
			Fprintf(w, "###### ###### ###### ###### ###### ######\n")
			Fprintf(w, "%s\n", strings.Replace(f1[0], "\r", "", -1))
			w.Flush()
			fd.Close()
		}

		terp.Run()
		if d.HasImage() {
			w.Header().Set("Content-Type", "image/png")
			d.WritePng(w)
		} else {
			w.Header().Set("Content-Type", "text/plain")
			Fprintf(w, "Use 'PRINT' or 'CALL triangle' statements to produce output.")
		}
	} else {
		dict := make(map[string]interface{})
		if f2, ok2 := req.Form["load"]; ok2 {
			code, err := ioutil.ReadFile(strings.Trim(f2[0], " \t\n\r"))
			s := "\n" + string(code)
			if err != nil {
				panic(err)
			}
			m := THRU_CRUNCH.FindStringSubmatch(s)
			if m != nil {
				s = m[1]
			}
			dict["Code"] = s
		} else {
			dict["Code"] = template.HTML(DEMO)
		}

		{
			names, err := filepath.Glob("*{33}.bas")
			if err != nil {
//.........这里部分代码省略.........
开发者ID:strickyak,项目名称:basic_basic,代码行数:101,代码来源:main.go


注:本文中的net/http.ResponseWriter.Flush方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。