Golang Request.Close Examples

Golang Request.Close - 30 examples found. These are the top rated real world Golang examples of net/http.Request.Close extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (h *healthcheck) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	proxyWriter, ok := rw.(utils.ProxyResponseWriter)
	var accessLogRecord *schema.AccessLogRecord
	if ok {
		alr := proxyWriter.Context().Value("AccessLogRecord")
		if alr == nil {
			h.logger.Error("AccessLogRecord not set on context", errors.New("failed-to-access-log-record"))
		}
		accessLogRecord = alr.(*schema.AccessLogRecord)
	}
	if h.userAgent == "" || r.Header.Get("User-Agent") == h.userAgent {
		rw.Header().Set("Cache-Control", "private, max-age=0")
		rw.Header().Set("Expires", "0")

		draining := atomic.LoadInt32(h.heartbeatOK) == 0
		if !draining {
			rw.WriteHeader(http.StatusOK)
			rw.Write([]byte("ok\n"))
			r.Close = true
			if ok {
				accessLogRecord.StatusCode = http.StatusOK
			}
		} else {
			rw.WriteHeader(http.StatusServiceUnavailable)
			r.Close = true
			if ok {
				accessLogRecord.StatusCode = http.StatusServiceUnavailable
			}
		}
		return
	}
	next(rw, r)
}
Example #2
0
func materialHandler(key string, w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		panic("expected GET")
	}

	untrustedDocName, err := url.QueryUnescape(r.URL.RawQuery)
	if err != nil {
		panic(err)
	}

	log.Printf("%v downloaded %v", key, untrustedDocName)

	// TODO(arjun): this is no longer a file path, but a URL fragment, I think
	docDir, docName := filepath.Split(untrustedDocName)
	if docDir != "" {
		log.Printf("%v SECURITY ERROR %v trying to read document %v", r.RemoteAddr,
			key, untrustedDocName)
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		return
	}

	log.Printf("%v %v downloaded %v", r.RemoteAddr, key, docName)
	w.Header().Add("Content-Disposition",
		fmt.Sprintf("inline; filename = %q", docName))
	w.Header().Add("Content-Type", "application/pdf")
	err = dept.DownloadFile(docName, w)
	if err != nil {
		log.Printf("dept.DownloadFile(%v, _) error: %v", docName, err)
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
	}
}
Example #3
0
func delHighlightHandler(key string, w http.ResponseWriter, r *http.Request) {
	var arg FetchCommentsEnv
	err := util.StringToJSON(key, &arg)
	if err != nil {
		log.Printf("FATAL ERROR decoding closure %v in setScoreHandler", key)
		w.WriteHeader(500)
		return
	}
	if r.Method != "POST" {
		log.Printf("%v SECURITY ERROR %v trying to %v to %v", r.RemoteAddr,
			arg.ReviewerId, r.Method, r.URL)
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		return
	}
	err = dept.DelHighlight(arg.AppId, string(arg.ReviewerId))
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		log.Printf("%v ERROR DelHighlight(%v, %v): %v", r.RemoteAddr, arg.AppId,
			arg.ReviewerId, err)
		return
	}
	w.WriteHeader(200)
}
Example #4
0
// Given a request, follow the segments through sitetree to find the page that is being requested. Doesn't
// understand actions, so just finds the page. Returns ID of SiteTree_Live record or 0 if it can't find a
// matching page.
// @todo Understand BaseController actions, or break on the furthest it gets up the tree
// @todo cache site tree
func findPageToRender(r *http.Request) (int, error) {
	siteCache := getSiteCache()
	if siteCache != nil {
		id, found := siteCache.findPageToRender(r)
		if found {
			// fmt.Printf("page cache hit %d\n", id)
			return id, nil
		}
	}

	s := strings.Trim(r.URL.Path, "/")
	path := strings.Split(s, "/")

	if len(path) == 0 || path[0] == "" {
		// find a home page ID
		r, e := orm.Query("select \"ID\" from \"SiteTree_Live\" where \"URLSegment\"='home' and \"ParentID\"=0")
		defer r.Close()

		if e != nil {
			return 0, e
		}
		if !r.Next() {
			return 0, nil
		}

		var ID int
		e = r.Scan(&ID)

		return ID, e
	}

	currParentID := 0
	for _, p := range path {
		r, e := orm.Query("select \"ID\",\"ParentID\" from \"SiteTree_Live\" where \"URLSegment\"='" + p + "' and \"ParentID\"=" + strconv.Itoa(currParentID))
		defer r.Close()

		if e != nil {
			return 0, e
		}
		if !r.Next() {
			return 0, nil
		}

		var ID, ParentID int
		e = r.Scan(&ID, &ParentID)
		currParentID = ID
	}

	// if we get to the end, we've found a matching ID in SiteTree_Live
	return currParentID, nil
}
Example #5
0
// OfferingList parse the yaml files from the repository configuration file defined in the seettings package
// And then returns it json representation
func OfferingList(r *http.Request) (int, []byte, error) {
	settings := settings.Get()
	rootRepository := settings.OfferingRepositoryPath
	var response []byte
	sendOffering := func(path string, info os.FileInfo, err error) error {
		stat, err := os.Stat(path)
		if err != nil {
			return err

		}

		if stat.IsDir() && path != rootRepository {
			return filepath.SkipDir

		}
		matched, err := filepath.Match("*.yaml", info.Name())
		if err != nil {
			return err // this is fatal.

		}
		if matched {
			r, err := os.Open(path)
			if err != nil {
				return err
			}
			data, err := ioutil.ReadAll(r)
			defer r.Close()
			if err != nil {
				return err
			}
			var nodeTypes map[string]toscalib.NodeType
			err = yaml.Unmarshal(data, &nodeTypes)
			if err != nil {
				return err
			}
			response, err = json.MarshalIndent(nodeTypes, "", "  ")
			if err != nil {
				return err
			}
		}
		return nil

	}
	err := filepath.Walk(rootRepository, sendOffering)
	if err != nil {
		log.Println(err)
		return http.StatusInternalServerError, nil, err
	}
	return http.StatusOK, response, nil
}
Example #6
0
func post(url_ string, oauthHeaders map[string]string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "POST"
	req.ProtoMajor = 1
	req.ProtoMinor = 1
	req.Close = true
	req.Header = map[string][]string{
		"Authorization": {"OAuth "},
	}
	req.TransferEncoding = []string{"chunked"}

	first := true
	for k, v := range oauthHeaders {
		if first {
			first = false
		} else {
			req.Header["Authorization"][0] += ",\n    "
		}
		req.Header["Authorization"][0] += k + "=\"" + v + "\""
	}

	req.URL, err = url.Parse(url_)
	if err != nil {
		return nil, err
	}

	return send(&req)
}