示例#1
0
func headersToWriter(h http.Header, w io.Writer) error {
	if err := h.Write(w); err != nil {
		return err
	}
	// ReadMIMEHeader expects a trailing newline
	_, err := w.Write([]byte("\r\n"))
	return err
}
示例#2
0
文件: chat.go 项目: prudhviy/pp_chat
func buildHTTPResponse(bufrw *bufio.ReadWriter) {
	var newHeader http.Header = make(http.Header)

	newHeader.Add("Access-Control-Allow-Origin", "*")
	newHeader.Add("Content-Type", "application/json; charset=UTF-8")
	newHeader.Add("Cache-Control", "no-cache")
	newHeader.Add("X-AppServer", "GoAPP")
	// write status line
	bufrw.WriteString("HTTP/1.1 200 OK" + "\r\n")
	// write headers
	_ = newHeader.Write(bufrw)
	// write a black line
	bufrw.WriteString("\n")
}
示例#3
0
文件: client.go 项目: bit4bit/remoton
func (c *SessionClient) dialTCP(service string, action string) (net.Conn, error) {

	burl, err := url.Parse(c.APIURL)
	if err != nil {
		return nil, err
	}
	burl.Path += fmt.Sprintf("%s/session/%s/conn/%s%s/tcp", c.Prefix, c.ID, service, action)
	req, err := http.NewRequest("GET", burl.String(), nil)
	req.Header.Set("X-Auth-Session", c.authToken)
	if err != nil {
		return nil, err
	}

	var conn net.Conn
	if burl.Scheme == "https" {
		conn, err = tls.Dial("tcp", burl.Host, c.TLSConfig)
	} else {
		conn, err = net.Dial("tcp", burl.Host)
	}
	if err != nil {
		return nil, err
	}

	br := bufio.NewReader(conn)
	bw := bufio.NewWriter(conn)
	bw.WriteString("GET " + burl.RequestURI() + " HTTP/1.1\r\n")
	bw.WriteString("Host: " + burl.Host + "\r\n")
	bw.WriteString("Connection: Upgrade\r\n")
	header := http.Header{}
	header.Set("X-Auth-Session", c.authToken)
	err = header.Write(bw)
	if err != nil {
		return nil, err
	}
	bw.WriteString("\r\n")
	if err = bw.Flush(); err != nil {
		return nil, err
	}

	resp, err := http.ReadResponse(br, &http.Request{Method: "GET"})
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("sessionClient.dialTCP: http response error %s", resp.Status)
	}

	return conn, nil
}
示例#4
0
文件: util.go 项目: Wessie/sirencast
// WriteHeader writes the HTTP status line and headers to writer `w`.
func WriteHeader(w io.Writer, h http.Header, code int) error {
	text := http.StatusText(code)
	if text == "" {
		return errors.New("header: invalid status code")
	}
	statusCode := strconv.Itoa(code)
	io.WriteString(w, "HTTP/1.0 "+statusCode+" "+text+"\r\n")

	if h != nil {
		if err := h.Write(w); err != nil {
			return err
		}
	}

	_, err := io.WriteString(w, "\r\n")
	return err
}
示例#5
0
func getHeaderBuffer(code int, headers http.Header) (*bytes.Buffer, error) {
	headerBuffer := &bytes.Buffer{}
	if _, err := headerBuffer.Write([]byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", code, http.StatusText(code)))); err != nil {
		return nil, err
	}

	//Write headers
	if err := headers.Write(headerBuffer); err != nil {
		return nil, err
	}
	// ReadMIMEHeader expects a trailing newline
	if _, err := headerBuffer.Write([]byte("\r\n")); err != nil {
		return nil, err
	}

	return headerBuffer, nil
}
示例#6
0
func NewHTTPClient(conn io.ReadWriteCloser, path string, header http.Header) (*Client, error) {
	if header == nil {
		header = make(http.Header)
	}
	header.Set("Accept", "application/vnd.flynn.rpc-hijack+gob")

	fmt.Fprintf(conn, "CONNECT %s HTTP/1.0\r\n", path)
	header.Write(conn)
	conn.Write([]byte("\r\n"))

	// Require successful HTTP response
	// before switching to RPC protocol.
	resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
	if err != nil || resp.Status != connected {
		if err == nil {
			err = errors.New("unexpected HTTP response: " + resp.Status)
		}
		return nil, err
	}
	return NewClient(conn), nil
}
示例#7
0
func (s *TestSuite) writeHeaders(headers http.Header) string {
	buf := &bytes.Buffer{}
	headers.Write(buf)

	return buf.String()
}
示例#8
0
// extraHeaders may be nil
// waitForContinue may be nil
func writeRequestFull(req *http.Request, w io.Writer, usingProxy bool, extraHeaders http.Header, waitForContinue func() bool) error {
	// Find the target host. Prefer the Host: header, but if that
	// is not given, use the host from the request URL.
	//
	// Clean the host, in case it arrives with unexpected stuff in it.
	host := cleanHost(req.Host)
	if host == "" {
		if req.URL == nil {
			return errMissingHost
		}
		host = cleanHost(req.URL.Host)
	}

	// According to RFC 6874, an HTTP client, proxy, or other
	// intermediary must remove any IPv6 zone identifier attached
	// to an outgoing URI.
	host = removeZone(host)

	ruri := req.URL.RequestURI()
	if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" {
		ruri = req.URL.Scheme + "://" + host + ruri
	} else if req.Method == "CONNECT" && req.URL.Path == "" {
		// CONNECT requests normally give just the host and port, not a full URL.
		ruri = host
	}
	// TODO(bradfitz): escape at least newlines in ruri?

	// Wrap the writer in a bufio Writer if it's not already buffered.
	// Don't always call NewWriter, as that forces a bytes.Buffer
	// and other small bufio Writers to have a minimum 4k buffer
	// size.
	var bw *bufio.Writer
	if _, ok := w.(io.ByteWriter); !ok {
		bw = bufio.NewWriter(w)
		w = bw
	}

	_, err := fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri)
	if err != nil {
		return err
	}

	// Header lines
	_, err = fmt.Fprintf(w, "Host: %s\r\n", host)
	if err != nil {
		return err
	}

	// Use the defaultUserAgent unless the Header contains one, which
	// may be blank to not send the header.
	userAgent := defaultUserAgent
	if _, ok := req.Header["User-Agent"]; ok {
		userAgent = req.Header.Get("User-Agent")
	}
	if userAgent != "" {
		_, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent)
		if err != nil {
			return err
		}
	}

	// Process Body,ContentLength,Close,Trailer
	tw, err := newTransferWriter(req)
	if err != nil {
		return err
	}
	err = tw.WriteHeader(w)
	if err != nil {
		return err
	}

	err = req.Header.WriteSubset(w, reqWriteExcludeHeader)
	if err != nil {
		return err
	}

	if extraHeaders != nil {
		err = extraHeaders.Write(w)
		if err != nil {
			return err
		}
	}

	_, err = io.WriteString(w, "\r\n")
	if err != nil {
		return err
	}

	// Flush and wait for 100-continue if expected.
	if waitForContinue != nil {
		if bw, ok := w.(*bufio.Writer); ok {
			err = bw.Flush()
			if err != nil {
				return err
			}
		}

		if !waitForContinue() {
			closeBody(req)
			return nil
		}
	}

	// Write body and trailer
	err = tw.WriteBody(w)
	if err != nil {
		return err
	}

	if bw != nil {
		return bw.Flush()
	}
	return nil
}
示例#9
0
/**
 * Prepares and dispatches a response via ResponseWriter for a publish API request
 */
func apiRequestPublish(w http.ResponseWriter, r *http.Request) {
	path := strings.Split(r.URL.Path, "/")

	statisticsIncrement(RUNTIME_PUBLISHES_RECEIVED)

	message, err := getMessageFromPost(r)
	if err != nil {
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE)
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE_400)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	auth, err := getAuthentication(r)
	if err != nil {
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE)
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE_401)
		header := http.Header{}
		header.Add("WWW-Authenticate", "Basic realm=hare")
		header.Add("Status", "HTTP/1.1 401")
		_ = header.Write(w)
		return
	}

	vhost := getUriValue(path, 1)
	if len(vhost) < 1 {
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE)
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE_404)
		http.NotFound(w, r)
		return
	}

	definition := &ConnectionDefinition{
		vhost,
		auth["user"],
		auth["pass"],
	}

	//Ensure we have a connection to VHOST
	_, _, err = GetConnection(*definition)
	if err != nil {
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE)
		if err == amqp.ErrVhost || err == amqp.ErrCredentials {
			statisticsIncrement(RUNTIME_PUBLISHES_FAILURE_403)
			w.WriteHeader(http.StatusForbidden)
			return
		} else {
			statisticsIncrement(RUNTIME_PUBLISHES_FAILURE_500)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		return
	}

	exchange := getUriValue(path, 2)
	routingKey := getUriValue(path, 3)

	if err := publish(*definition, exchange, routingKey, *message); err != nil {
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE)
		statisticsIncrement(RUNTIME_PUBLISHES_FAILURE_500)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	statisticsIncrement(RUNTIME_PUBLISHES_SUCCESS)
	w.WriteHeader(http.StatusNoContent)
	return
}
示例#10
0
文件: goHttp.go 项目: keysonZZZ/kmg
func HeaderToString(header http.Header) (s string) {
	buf := &bytes.Buffer{}
	header.Write(buf)
	return string(buf.Bytes())
}
示例#11
0
// extraHeaders may be nil
func write(req *http.Request, w io.Writer, usingProxy bool, extraHeaders http.Header) error {
	host := req.Host
	if host == "" {
		if req.URL == nil {
			return errors.New("http: Request.Write on Request with no Host or URL set")
		}
		host = req.URL.Host
	}

	ruri := req.URL.RequestURI()
	if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" {
		ruri = req.URL.Scheme + "://" + host + ruri
	} else if req.Method == "CONNECT" && req.URL.Path == "" {
		// CONNECT requests normally give just the host and port, not a full URL.
		ruri = host
	}
	// TODO(bradfitz): escape at least newlines in ruri?

	bw := bufio.NewWriter(w)
	fmt.Fprintf(bw, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri)

	// Header lines
	fmt.Fprintf(bw, "Host: %s\r\n", host)

	// Use the defaultUserAgent unless the Header contains one, which
	// may be blank to not send the header.
	userAgent := defaultUserAgent
	if req.Header != nil {
		if ua := req.Header["User-Agent"]; len(ua) > 0 {
			userAgent = ua[0]
		}
	}
	if userAgent != "" {
		fmt.Fprintf(bw, "User-Agent: %s\r\n", userAgent)
	}

	// Process Body,ContentLength,Close,Trailer
	tw, err := newTransferWriter(req)
	if err != nil {
		return err
	}
	err = tw.WriteHeader(bw)
	if err != nil {
		return err
	}

	// TODO: split long values?  (If so, should share code with Conn.Write)
	err = req.Header.WriteSubset(bw, reqWriteExcludeHeader)
	if err != nil {
		return err
	}

	if extraHeaders != nil {
		err = extraHeaders.Write(bw)
		if err != nil {
			return err
		}
	}

	io.WriteString(bw, "\r\n")

	// Write body and trailer
	err = tw.WriteBody(bw)
	if err != nil {
		return err
	}

	return bw.Flush()
}
示例#12
0
func Upload(url string, file_path string, header http.Header, check string, conc int) error {

	var md5Hash hash.Hash

	r, err := os.Open(file_path)
	if err != nil {
		if file_path == "" {
			r = os.Stdin
		} else {
			return err
		}
	}

	defer r.Close()

	log.Println("Check option: ", check)

	if check == "metadata" {
		// precalculate md5 for http header
		md5Hash, err := md5Calc(r)
		if err != nil {
			return err
		}
		if header == nil {
			header = make(http.Header)
		}

		md5Header := fmt.Sprintf("%x", md5Hash.Sum(nil))
		header.Set(checkSumHeader, md5Header)
		log.Println("POST REQ HEADER:")
		header.Write(os.Stderr)
	}
	k := Keys{AccessKey: os.Getenv("AWS_ACCESS_KEY"),
		SecretKey: os.Getenv("AWS_SECRET_KEY"),
	}
	s3_ := New("", k)
	b := s3_.Bucket("rm-dev-repos")
	key, _ := url_.Parse(url)
	path := key.Path
	c := DefaultConfig
	c.Concurrency = conc
	w, err := b.PutWriter(path, header, c)

	if err != nil {
		return err
	}
	mw := io.MultiWriter(w)

	if check == "file" {
		md5Hash = md5.New()
		mw = io.MultiWriter(md5Hash, w)

	}

	if _, err := io.Copy(mw, r); err != nil {
		return err
	}
	if err := w.Close(); err != nil {
		return err
	}

	// Write md5 to file and upload
	if check == "file" {
		if err := md5FileUpload(md5Hash, url, b); err != nil {
			return err
		}
	}

	return nil
}