Esempio n. 1
2
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
	host := "127.0.0.1"
	port := "80"
	rawurl := "http://" + host + ":" + port + path
	url_, _ := url.Parse(rawurl)

	proto := "HTTP/1.1"

	if headers == nil {
		headers = map[string][]string{}
	}

	headers["User-Agent"] = []string{"web.go test"}
	if method == "POST" {
		headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
		if headers["Content-Type"] == nil {
			headers["Content-Type"] = []string{"text/plain"}
		}
	}

	req := http.Request{Method: method,
		RawURL: rawurl,
		URL:    url_,
		Proto:  proto,
		Host:   host,
		Header: http.Header(headers),
		Body:   ioutil.NopCloser(bytes.NewBufferString(body)),
	}

	for _, cookie := range cookies {
		req.AddCookie(cookie)
	}
	return &req
}
Esempio n. 2
0
func getResponse(rawUrl string, req *http.Request) (*http.ClientConn, *http.Response, os.Error) {
	url, err := url.Parse(rawUrl)
	if url.Scheme == "" {
		rawUrl = "http://" + rawUrl
		url, err = url.Parse(rawUrl)
	}

	if err != nil {
		return nil, nil, err
	}
	req.URL = url
	if debugprint {
		dump, err := http.DumpRequest(req, true)
		if err != nil {
			println(err.String())
		}
		print(string(dump))
	}

	conn, err := newConn(url)
	if err != nil {
		println(err.String())
		return nil, nil, err
	}

	resp, err := conn.Do(req)
	if err != nil {
		if err != http.ErrPersistEOF {
			return nil, nil, err
		}
	}
	return conn, resp, nil
}
func ResolveRelative(basestr, relstr string) string {
	u, _ := url.Parse(basestr)
	rel, _ := url.Parse(relstr)
	u = u.ResolveReference(rel)
	us := u.String()
	us = strings.Replace(us, "%7B", "{", -1)
	us = strings.Replace(us, "%7D", "}", -1)
	return us
}
Esempio n. 4
0
func resolveRelative(basestr, relstr string) string {
	u, err := url.Parse(basestr)
	if err != nil {
		panicf("Error parsing base URL %q: %v", basestr, err)
	}
	rel, err := url.Parse(relstr)
	if err != nil {
		panicf("Error parsing relative URL %q: %v", relstr, err)
	}
	u = u.ResolveReference(rel)
	return u.String()
}
Esempio n. 5
0
// RoundTrip implements the RoundTripper interface.
func (t *Transport) RoundTrip(req *Request) (resp *Response, err os.Error) {
	if req.URL == nil {
		if req.URL, err = url.Parse(req.RawURL); err != nil {
			return
		}
	}
	if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
		t.lk.Lock()
		var rt RoundTripper
		if t.altProto != nil {
			rt = t.altProto[req.URL.Scheme]
		}
		t.lk.Unlock()
		if rt == nil {
			return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
		}
		return rt.RoundTrip(req)
	}

	cm, err := t.connectMethodForRequest(req)
	if err != nil {
		return nil, err
	}

	// Get the cached or newly-created connection to either the
	// host (for http or https), the http proxy, or the http proxy
	// pre-CONNECTed to https server.  In any case, we'll be ready
	// to send it requests.
	pconn, err := t.getConn(cm)
	if err != nil {
		return nil, err
	}

	return pconn.roundTrip(req)
}
Esempio n. 6
0
/*
Dial opens a new client connection to a Web Socket.

A trivial example client:

	package main

	import (
		"websocket"
		"strings"
	)

	func main() {
	 	ws, err := websocket.Dial("ws://localhost/ws", "", "http://localhost/");
	 	if err != nil {
			panic("Dial: " + err.String())
		}
		if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
			panic("Write: " + err.String())
		}
		var msg = make([]byte, 512);
		if n, err := ws.Read(msg); err != nil {
			panic("Read: " + err.String())
		}
		// use msg[0:n]
	}
*/
func Dial(url_, protocol, origin string) (ws *Conn, err os.Error) {
	var client net.Conn

	parsedUrl, err := url.Parse(url_)
	if err != nil {
		goto Error
	}

	switch parsedUrl.Scheme {
	case "ws":
		client, err = net.Dial("tcp", parsedUrl.Host)

	case "wss":
		client, err = tls.Dial("tcp", parsedUrl.Host, nil)

	default:
		err = ErrBadScheme
	}
	if err != nil {
		goto Error
	}

	ws, err = newClient(parsedUrl.RawPath, parsedUrl.Host, origin, url_, protocol, client, handshake)
	if err != nil {
		goto Error
	}
	return

Error:
	return nil, &DialError{url_, protocol, origin, err}
}
Esempio n. 7
0
func OpenRaw(uarel string) (*Conn, os.Error) {
	u, err := url.Parse(uarel)
	if err != nil {
		return nil, err
	}

	nc, err := net.Dial("tcp", u.Host)
	if err != nil {
		return nil, err
	}

	// TODO: use pass
	user, _, err := url.UnescapeUserinfo(u.RawUserinfo)
	if err != nil {
		return nil, err
	}

	params := make(proto.Values)
	params.Set("user", user)
	if u.Path != "" {
		params.Set("database", path.Base(u.Path))
	}

	return New(nc, params)
}
Esempio n. 8
0
func post(theUrl string, oauthHeaders map[string]string) (r *http.Response, err os.Error) {
	var req http.Request
	var authorization string = "OAuth "
	req.Method = "POST"
	req.ProtoMajor = 1
	req.ProtoMinor = 1
	req.Close = true
	req.Header = http.Header{}

	req.TransferEncoding = []string{"chunked"}

	first := true
	for k, v := range oauthHeaders {
		if first {
			first = false
		} else {
			authorization += ",\n    "
		}
		authorization += k + "=\"" + v + "\""
	}

	req.Header.Add("Authorization", authorization)

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

	return send(&req)
}
Esempio n. 9
0
func CachePath(m *Module, cacheroot string) (string, error) {
	source, err := url.Parse(m.Source)
	if err == nil {
		return filepath.Join(cacheroot, source.Host, source.Path), nil
	}
	return "", err
}
Esempio n. 10
0
func GetParameters(urlToParse string) url.Values {
	urlObj, err := url.Parse(urlToParse)
	if err != nil {
		log.Panic("Couldn't parse URL parameters from string")
	}
	return urlObj.Query()
}
Esempio n. 11
0
func post(url_ string, oauthHeaders map[string]string) (r *http.Response, err os.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)
}
Esempio n. 12
0
func TestHostHandlers(t *testing.T) {
	for _, h := range handlers {
		Handle(h.pattern, stringHandler(h.msg))
	}
	ts := httptest.NewServer(nil)
	defer ts.Close()

	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	cc := NewClientConn(conn, nil)
	for _, vt := range vtests {
		var r *Response
		var req Request
		if req.URL, err = url.Parse(vt.url); err != nil {
			t.Errorf("cannot parse url: %v", err)
			continue
		}
		if err := cc.Write(&req); err != nil {
			t.Errorf("writing request: %v", err)
			continue
		}
		r, err := cc.Read(&req)
		if err != nil {
			t.Errorf("reading response: %v", err)
			continue
		}
		s := r.Header.Get("Result")
		if s != vt.expected {
			t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
		}
	}
}
Esempio n. 13
0
// NewRequest returns a new Request given a method, URL, and optional body.
func NewRequest(method, urlStr string, body io.Reader) (*Request, os.Error) {
	u, err := url.Parse(urlStr)
	if err != nil {
		return nil, err
	}
	rc, ok := body.(io.ReadCloser)
	if !ok && body != nil {
		rc = ioutil.NopCloser(body)
	}
	req := &Request{
		Method:     method,
		URL:        u,
		Proto:      "HTTP/1.1",
		ProtoMajor: 1,
		ProtoMinor: 1,
		Header:     make(Header),
		Body:       rc,
		Host:       u.Host,
	}
	if body != nil {
		switch v := body.(type) {
		case *strings.Reader:
			req.ContentLength = int64(v.Len())
		case *bytes.Buffer:
			req.ContentLength = int64(v.Len())
		}
	}

	return req, nil
}
Esempio n. 14
0
func TestLocationResponse(t *testing.T) {
	for i, tt := range responseLocationTests {
		res := new(Response)
		res.Header = make(Header)
		res.Header.Set("Location", tt.location)
		if tt.requrl != "" {
			res.Request = &Request{}
			var err os.Error
			res.Request.URL, err = url.Parse(tt.requrl)
			if err != nil {
				t.Fatalf("bad test URL %q: %v", tt.requrl, err)
			}
		}

		got, err := res.Location()
		if tt.wantErr != nil {
			if err == nil {
				t.Errorf("%d. err=nil; want %q", i, tt.wantErr)
				continue
			}
			if g, e := err.String(), tt.wantErr.String(); g != e {
				t.Errorf("%d. err=%q; want %q", i, g, e)
				continue
			}
			continue
		}
		if err != nil {
			t.Errorf("%d. err=%q", i, err)
			continue
		}
		if g, e := got.String(), tt.want; g != e {
			t.Errorf("%d. Location=%q; want %q", i, g, e)
		}
	}
}
Esempio n. 15
0
func (self *HTTPClient) defaultRequest() (req *http.Request, err os.Error) {
	var h http.Header = map[string][]string{}
	req = new(http.Request)
	req.Method = self.method
	if self.contentType != "" {
		headers := map[string]string{"Content-Type": self.contentType}
		for k, v := range headers {
			h.Add(k, v)
		}
	}
	//Request should have a Header, otherwise the
	//transport will not work.
	req.Header = h

	req.ProtoMajor = 1
	req.ProtoMinor = 1
	if self.cookie.Name != "" && self.cookie.Value != "" {
		req.AddCookie(&http.Cookie{Name: self.cookie.Name, Value: self.cookie.Value})
	}

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

}
Esempio n. 16
0
// make a GET request with a fake user agent
// this is definitely not for those undocumented Google APIs
func getUserAgent(urlstr string) ([]byte, os.Error) {
	urlobj, err := url.Parse(urlstr)
	if err != nil {
		return nil, err
	}

	conn, err := net.Dial("tcp", urlobj.Host+":http")
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequest("GET", urlstr, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("User-Agent", "Mozilla/5.0")

	httpconn := http.NewClientConn(conn, nil)
	response, err := httpconn.Do(req)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()

	b, err := ioutil.ReadAll(response.Body)
	return b, err
}
Esempio n. 17
0
func ParseURL(url_ string) (parsed_url *url.URL) {
	parsed_url, err := url.Parse(url_)
	if err != nil {
		log.Fatal(err)
	}
	return parsed_url
}
Esempio n. 18
0
func TestQuery(t *testing.T) {
	req := &Request{Method: "GET"}
	req.URL, _ = url.Parse("http://www.google.com/search?q=foo&q=bar")
	if q := req.FormValue("q"); q != "foo" {
		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
	}
}
Esempio n. 19
0
// starts service based on the given configuration file
// required parameters:  sections for each domain object accepted (e.g. jobs for /jobs)
// optional parameters:  [domain_group] contentType=application/json (default)
func StartREST() {
	domains := configFile.GetSections()
	for _, domain := range domains {
		if domain != "default" {
			dbhost := GetRequiredString(configFile, domain, "dbHost")
			dbstore := GetRequiredString(configFile, domain, "dbName")
			jsonParameter := GetRequiredString(configFile, domain, "jsonParameter")
			proxyTarget, err := configFile.GetString(domain, "serviceProxy")
			if err != nil {
				logger.Warn(err)
				logger.Print("no service proxy configured")
			}

			logger.Debug("starting REST: Domain [%v]", domain)
			logger.Debug("starting REST: MongoDB at [%v,%v]", dbhost, dbstore)
			logger.Debug("starting REST: JSON Param [%v]", jsonParameter)
			logger.Debug("starting REST: PostCommit Hook [%v]", proxyTarget)

			targetUrl, _ := url.Parse(proxyTarget)
			store := &JsonStore{Domain: domain, Host: dbhost, Database: dbstore}
			rest.Resource(domain, RestJsonMongo{Store: store, Target: targetUrl, JsonParam: jsonParameter})

			contentType, err := configFile.GetString(domain, "contentType")
			if err != nil {
				logger.Warn(err)
				contentType = "application/json"
			}
			logger.Debug("starting REST: Content Type [%v]", contentType)
			rest.ResourceContentType(domain, contentType)
		}
	}
}
Esempio n. 20
0
func mustParseURL(s string) *url.URL {
	u, err := url.Parse(s)
	if err != nil {
		panic(fmt.Sprintf("Error parsing URL %q: %v", s, err))
	}
	return u
}
Esempio n. 21
0
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *Request {
	host := "127.0.0.1"
	port := "80"
	rawurl := "http://" + host + ":" + port + path
	url_, _ := url.Parse(rawurl)

	proto := "HTTP/1.1"
	useragent := "web.go test"

	if headers == nil {
		headers = map[string][]string{}
	}

	if method == "POST" {
		headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
		if headers["Content-Type"] == nil {
			headers["Content-Type"] = []string{"text/plain"}
		}
	}

	req := Request{Method: method,
		RawURL:    rawurl,
		Cookie:    cookies,
		URL:       url_,
		Proto:     proto,
		Host:      host,
		UserAgent: useragent,
		Headers:   headers,
		Body:      bytes.NewBufferString(body),
	}

	return &req
}
Esempio n. 22
0
func (s *TestHTTPServer) Start() {
	if s.started {
		return
	}
	s.started = true

	s.request = make(chan *http.Request, 64)
	s.response = make(chan *testResponse, 64)
	s.pending = make(chan bool, 64)

	url, _ := url.Parse(s.URL)
	go http.ListenAndServe(url.Host, s)

	s.PrepareResponse(202, nil, "Nothing.")
	fmt.Fprintf(os.Stderr, "\nWaiting for the fake server to be up...")
	for {
		resp, err := http.Get(s.URL)
		if err == nil && resp.StatusCode == 202 {
			break
		}

		time.Sleep(1e8)
	}

	fmt.Fprintf(os.Stderr, "Done\n")
	s.WaitRequest()
}
Esempio n. 23
0
File: sns.go Progetto: supr/sns
func (sns *SNS) query(topic *Topic, message *Message, params map[string]string, resp interface{}) os.Error {
	params["Timestamp"] = time.UTC().Format(time.RFC3339)
	url_, err := url.Parse(sns.Region.SNSEndpoint)
	if err != nil {
		return err
	}

	sign(sns.Auth, "GET", "/", params, url_.Host)
	url_.RawQuery = multimap(params).Encode()
	r, err := http.Get(url_.String())
	if err != nil {
		return err
	}
	defer r.Body.Close()

	//dump, _ := http.DumpResponse(r, true)
	//println("DUMP:\n", string(dump))
	//return nil

	if r.StatusCode != 200 {
		return buildError(r)
	}
	err = xml.Unmarshal(r.Body, resp)
	return err
}
Esempio n. 24
0
func NewTHttpPostClient(urlstr string) (TTransport, os.Error) {
	parsedURL, err := url.Parse(urlstr)
	if err != nil {
		return nil, err
	}
	buf := make([]byte, 0, 1024)
	return &THttpClient{url: parsedURL, requestBuffer: bytes.NewBuffer(buf)}, nil
}
Esempio n. 25
0
func (b Bookmark) FaviconURL() string {
	domain := ""
	u, err := url.Parse(b.URL)
	if err == nil {
		domain = u.Host
	}
	return "http://www.google.com/s2/u/0/favicons?domain=" + domain
}
Esempio n. 26
0
func (client *Client) redirectUrl(code string) string {
	//  We assume that the string is of the proper format; safe assumption since we reject invalid uris when saving
	baseUrl, _ := url.Parse(encryption.AESDecrypt(client.Redirect, "oauthclient.redirect"))
	query := baseUrl.Query()
	query.Add("code", code)
	baseUrl.RawQuery = query.Encode()
	return baseUrl.String()
}
Esempio n. 27
0
func YadisRequest(url_ string, method string) (resp *http.Response, err os.Error) {
	resp = nil

	var request = new(http.Request)
	var client = new(http.Client)
	var Header = make(http.Header)

	request.Method = method
	request.RawURL = url_

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

	// Common parameters
	request.Proto = "HTTP/1.0"
	request.ProtoMajor = 1
	request.ProtoMinor = 0
	request.ContentLength = 0
	request.Close = true

	Header.Add("Accept", "application/xrds+xml")
	request.Header = Header

	// Follow a maximum of 5 redirections
	for i := 0; i < 5; i++ {
		response, err := client.Do(request)

		if err != nil {
			return nil, err
		}
		if response.StatusCode == 301 || response.StatusCode == 302 || response.StatusCode == 303 || response.StatusCode == 307 {
			location := response.Header.Get("Location")
			request.RawURL = location
			request.URL, err = url.Parse(location)
			if err != nil {
				return
			}
		} else {
			return response, nil
		}
	}
	return nil, os.NewError("Too many redirections")
}
Esempio n. 28
0
func New(auth aws.Auth) *MTurk {
	mt := &MTurk{Auth: auth}
	var err os.Error
	mt.URL, err = url.Parse(MTURK_URL)
	if err != nil {
		panic(err.String())
	}
	return mt
}
Esempio n. 29
0
// Redirect replies to the request with a redirect to url,
// which may be a path relative to the request path.
func Redirect(w ResponseWriter, r *Request, urlStr string, code int) {
	if u, err := url.Parse(urlStr); err == nil {
		// If url was relative, make absolute by
		// combining with request path.
		// The browser would probably do this for us,
		// but doing it ourselves is more reliable.

		// NOTE(rsc): RFC 2616 says that the Location
		// line must be an absolute URI, like
		// "http://www.google.com/redirect/",
		// not a path like "/redirect/".
		// Unfortunately, we don't know what to
		// put in the host name section to get the
		// client to connect to us again, so we can't
		// know the right absolute URI to send back.
		// Because of this problem, no one pays attention
		// to the RFC; they all send back just a new path.
		// So do we.
		oldpath := r.URL.Path
		if oldpath == "" { // should not happen, but avoid a crash if it does
			oldpath = "/"
		}
		if u.Scheme == "" {
			// no leading http://server
			if urlStr == "" || urlStr[0] != '/' {
				// make relative path absolute
				olddir, _ := path.Split(oldpath)
				urlStr = olddir + urlStr
			}

			var query string
			if i := strings.Index(urlStr, "?"); i != -1 {
				urlStr, query = urlStr[:i], urlStr[i:]
			}

			// clean up but preserve trailing slash
			trailing := urlStr[len(urlStr)-1] == '/'
			urlStr = path.Clean(urlStr)
			if trailing && urlStr[len(urlStr)-1] != '/' {
				urlStr += "/"
			}
			urlStr += query
		}
	}

	w.Header().Set("Location", urlStr)
	w.WriteHeader(code)

	// RFC2616 recommends that a short note "SHOULD" be included in the
	// response because older user agents may not understand 301/307.
	// Shouldn't send the response for POST or HEAD; that leaves GET.
	if r.Method == "GET" {
		note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n"
		fmt.Fprintln(w, note)
	}
}
Esempio n. 30
0
func NewServerAuth(baseUrlStr string) (s *ServerAuth, err os.Error) {
	baseUrl, err := url.Parse(baseUrlStr)
	if err != nil {
		return
	}
	s = &ServerAuth{
		baseUrl: *baseUrl,
	}
	return
}