Esempio n. 1
0
func (t *Template) respond(req *web.Request, status int, value interface{}, kvs ...string) {
	header := web.NewHeader(kvs...)
	header.Set(web.HeaderContentType, t.mimeType)
	err := t.t.Execute(req.Responder.Respond(status, header), value)
	if err != nil {
		log.Println("Error executing template", err)
	}
}
Esempio n. 2
0
			return
		}
	}
}

var webSocketTests = []struct {
	in     string
	header web.Header
	fail   bool
}{
	{in: "", fail: true},
	{
		header: web.NewHeader(
			"Connection", "Upgrade",
			"Origin", "http://*****:*****@991HP I {2",
			"Sec-Websocket-Key1", "284<qQA84i92708  /"),
		in: "P\u05e4>mX\x18k",
	},
	{
		header: web.NewHeader(
			"Connection", "Upgrade",
			"Origin", "http://*****:*****@991HP I {2",
			"Sec-Websocket-Key1", "284<qQA84i92708  /"),
		in: "P\u05e4>mX\x18k\x00Hello\xff",
	},
Esempio n. 3
0
func (ts *TwitterStream) connect() (myErr *NetError) {
	var err error
	log.Println("twitterstream: connecting to", ts.urlStr)

	u, err := url.Parse(ts.urlStr)
	if err != nil {
		panic("bad url: " + ts.urlStr)
	}

	addr := u.Host
	if strings.LastIndex(addr, ":") <= strings.LastIndex(addr, "]") {
		if u.Scheme == "http" {
			addr = addr + ":80"
		} else {
			addr = addr + ":443"
		}
	}

	params := url.Values{}
	for key, values := range ts.params {
		params[key] = values
	}
	ts.oauthClient.SignParam(ts.accessToken, "POST", ts.urlStr, params)

	body := params.Encode()

	header := web.NewHeader(
		web.HeaderHost, u.Host,
		web.HeaderContentLength, strconv.Itoa(len(body)),
		web.HeaderContentType, "application/x-www-form-urlencoded")

	var request bytes.Buffer
	request.WriteString("POST ")
	request.WriteString(u.RawPath)
	request.WriteString(" HTTP/1.1\r\n")
	header.WriteHttpHeader(&request)
	request.WriteString(body)

	if u.Scheme == "http" {
		ts.conn, err = net.Dial("tcp", addr)
		if err != nil {
			myErr = &NetError{"connect: dial failed", true, err}
			ts.error(myErr.text, err)
			return
		}
	} else {
		ts.conn, err = tls.Dial("tcp", addr, nil)
		if err != nil {
			ts.conn = nil
			myErr = &NetError{"connect: dial failed", true, err}
			ts.error(myErr.text, err)
			return
		}
		if err = ts.conn.(*tls.Conn).VerifyHostname(addr[:strings.LastIndex(addr, ":")]); err != nil {
			myErr = &NetError{"connect: could not verify host", true, err}
			ts.error(myErr.text, err)
			return
		}
	}

	// Set timeout to detect dead connection. Twitter sends at least one line
	// to the response every 30 seconds.
	err = ts.conn.SetReadTimeout(int64(60 * time.Second))
	if err != nil {
		myErr = &NetError{"connect: set read timeout failed", true, err}
		ts.error(myErr.text, err)
		return
	}

	if _, err := ts.conn.Write(request.Bytes()); err != nil {
		myErr = &NetError{"connect: error writing request", true, err}
		ts.error(myErr.text, err)
		return
	}

	ts.r, _ = bufio.NewReaderSize(ts.conn, 8192)
	p, err := ts.r.ReadSlice('\n')
	if err != nil {
		myErr = &NetError{"connect: error reading response", true, err}
		ts.error(myErr.text, err)
		return
	}

	m := responseLineRegexp.FindSubmatch(p)
	if m == nil {
		myErr = &NetError{"connect: bad response line", true, nil}
		ts.error(myErr.text, err)
		return
	}

	for {
		p, err = ts.r.ReadSlice('\n')
		if err != nil {
			myErr = &NetError{"connect: error reading header", true, err}
			ts.error(myErr.text, err)
			return
		}
		if len(p) <= 2 {
			break
		}
	}

	if string(m[1]) != "200" {
		p, _ := ioutil.ReadAll(ts.r)
		log.Println(string(p))
		switch string(m[1]) {
		case "401", "403":
			myErr = &NetError{"connect: bad response code " + string(m[1]), false, nil}
		default:
			myErr = &NetError{"connect: bad response code " + string(m[1]), true, nil}
		}
		ts.error(myErr.text, err)
		return
	}

	ts.chunkState = stateStart

	log.Println("twitterstream: connected to", ts.urlStr)
	return
}
Esempio n. 4
0
func dial(urlString string) (net.Conn, *bufio.Reader, error) {
	u, err := url.Parse(urlString)
	if err != nil {
		return nil, nil, err
	}

	addr := u.Host
	if strings.LastIndex(addr, ":") <= strings.LastIndex(addr, "]") {
		if u.Scheme == "http" {
			addr = addr + ":80"
		} else {
			addr = addr + ":443"
		}
	}

	header := web.NewHeader(web.HeaderHost, u.Host)

	var request bytes.Buffer
	request.WriteString("GET ")
	request.WriteString(u.RawPath)
	request.WriteString(" HTTP/1.1\r\n")
	header.WriteHttpHeader(&request)

	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, nil, err
	}

	if _, err := conn.Write(request.Bytes()); err != nil {
		conn.Close()
		return nil, nil, err
	}

	r, _ := bufio.NewReaderSize(conn, 512)
	p, err := r.ReadSlice('\n')
	if err != nil {
		conn.Close()
		return nil, nil, err
	}

	m := responseLineRegexp.FindSubmatch(p)
	if m == nil {
		conn.Close()
		return nil, nil, errors.New("bad response")
	}

	for {
		p, err = r.ReadSlice('\n')
		if err != nil {
			conn.Close()
			return nil, nil, err
		}
		if len(p) <= 2 {
			break
		}
	}

	if string(m[1]) != "200" {
		p, _ := ioutil.ReadAll(r)
		log.Println(string(p))
		conn.Close()
		return nil, nil, errors.New("bad response")
	}
	return conn, r, nil
}