Esempio n. 1
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
}
Esempio n. 2
0
File: main.go Progetto: tobi/tinny
func pipeRequestResponse(server, client net.Conn) os.Error {

	// Read request from wire
	req, err := http.ReadRequest(bufio.NewReader(client))
	if err != nil {
		return err
	}

	rawReq, err := http.DumpRequest(req, true)
	if err != nil {
		return err
	}

	// forward it on
	server.Write(rawReq)

	// Read response from wire
	resp, err := http.ReadResponse(bufio.NewReader(server), req)
	if err != nil {
		return err
	}

	rawResp, err := http.DumpResponse(resp, true)
	if err != nil {
		return err
	}

	log.Printf("%s %s [%s]", req.Method, req.RawURL, resp.Status)

	// forward it on
	client.Write(rawResp)

	return nil
}
Esempio n. 3
0
File: http.go Progetto: vdobler/ft
// Dump request req in wire format to dump if non nil.
func dumpReq(req *http.Request, dump io.Writer) {
	if dump != nil {
		rd, err := http.DumpRequest(req, true)
		if err == nil {
			dump.Write(rd)
			dump.Write([]byte("\r\n\r\n--------------------------------------------------------------------------------------\r\n"))
			dump.Write([]byte("--------------------------------------------------------------------------------------\r\n\r\n\r\n"))
		} else {
			error("Cannot dump request: %s", err.String())
		}
	}
}
Esempio n. 4
0
func TestCode2(c *http.Conn, req *http.Request) {
	dump, err := http.DumpRequest(req, true)
	if err != nil {
		io.WriteString(c, err.String())
		return
	}
	_, err = c.Write(dump)
	if err != nil {
		io.WriteString(c, err.String())
		return
	}
	log.Stdout(string(dump))
}
Esempio n. 5
0
func (client *Client) Fetch(req *http.Request) (result *FetchResult) {
	creds := req.URL.RawUserinfo
	_, has_auth_header := req.Header["Authorization"]
	if len(creds) > 0 && !has_auth_header {
		encoded := make([]byte, base64.URLEncoding.EncodedLen(len(creds)))
		base64.URLEncoding.Encode(encoded, []byte(creds))
		if req.Header == nil {
			req.Header = make(map[string]string)
		}
		req.Header["Authorization"] = "Basic " + string(encoded)
	}

	// debug
	if false {
		dump, _ := http.DumpRequest(req, true)
		print(string(dump))
	}

	resp, err := client.Request(req)
	if err != nil {
		return ErrorResult(req.URL.Raw, err.String())
	}

	// Prevent simultaneous IO from different goroutines.
	client.lk.Lock()
	defer client.lk.Unlock()

	var buf bytes.Buffer
	_, err = io.Copy(&buf, resp.Body)
	responseBody := buf.Bytes()
	if err != nil {
		return ErrorResult(req.URL.Raw, err.String())
	}
	resp.Body.Close()

	return &FetchResult{
		Url:        req.URL.Raw,
		Success:    true,
		Status:     resp.Status,
		StatusCode: resp.StatusCode,
		Body:       responseBody,
		Headers:    resp.Header,
	}
}
Esempio n. 6
0
func TestUpdateUserAccount1(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1000)
    if len(accessKeys) == 0 {
        t.Error("Expected to find at least one access key stored.")
    }
    accessKey := accessKeys[0]
    otherUser := gw
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "GW")
    jsonobj.Set("email", "*****@*****.**")
    jsonobj.Set("address", "Pre-White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/" + otherUser.Id, bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
    reqbytes, _ := http.DumpRequest(req, true)
    t.Log("Request is:\n", string(reqbytes), "\n\n")
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    t.Log("Response is:\n", resp.String(), "\n\n")
    if resp.StatusCode != http.StatusOK {
        t.Error("Expected ", http.StatusOK, " status code but received ", resp.StatusCode)
    }
    if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
        t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
    }
    user := new(dm.User)
    obj := jsonhelper.NewJSONObject()
    err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
    user.InitFromJSONObject(obj.GetAsObject("result"))
    if err != nil {
        t.Error("Error while unmarshaling JSON: ", err.String())
    }
    if obj.GetAsString("status") != "success" {
        t.Error("Expected status = \"success\", but was \"", obj.GetAsString("status"), "\"")
    }
    if user.Name != otherUser.Name {
        t.Error("Expected name = \"", otherUser.Name, "\", but was ", user.Name)
    }
    if user.Username != otherUser.Username {
        t.Error("Expected username = \"", otherUser.Username, "\", but was ", user.Username)
    }
    if user.Email != otherUser.Email {
        t.Error("Expected email = \"", otherUser.Email, "\", but was ", user.Email)
    }
    if user.PhoneNumber != otherUser.PhoneNumber {
        t.Error("Expected phone_number = \"", otherUser.PhoneNumber, "\", but was ", user.PhoneNumber)
    }
    if user.Address != otherUser.Address {
        t.Error("Expected address = \"", otherUser.Address, "\", but was ", user.Address)
    }
    if user.Role != otherUser.Role {
        t.Error("Expected role = ", otherUser.Role, " but was ", user.Role)
    }
    if user.Id != otherUser.Id {
        t.Error("Expected id to be ", otherUser.Id, ", but was ", user.Id)
    }
    if theuser, err := ds.RetrieveUserAccountById(otherUser.Id); err != nil || theuser == nil {
        if theuser == nil {
            t.Error("Unable to find User account by id ", otherUser.Id)
        }
        if err != nil {
            t.Error("Error trying to find user account by id: ", err.String())
        }
    }
    if theuser, err := ds.FindUserAccountByUsername(otherUser.Username); err != nil || theuser == nil {
        if theuser == nil {
            t.Error("Unable to find User account by username ", otherUser.Username)
        }
        if err != nil {
            t.Error("Error trying to find user account by username: "******"Found ", len(theusers), " User accounts by email for ", otherUser.Email, " rather than 1: ", theusers)
        }
        if err != nil {
            t.Error("Error trying to find user accounts by email: ", err.String())
        }
    }
}