本文整理汇总了Golang中net/url.ParseRequestURI函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseRequestURI函数的具体用法?Golang ParseRequestURI怎么用?Golang ParseRequestURI使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseRequestURI函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewVAClient
// NewVAClient returns a new empty client to authenticate against the vCloud Air
// service, the vCloud Air endpoint can be overridden by setting the
// VCLOUDAIR_ENDPOINT environment variable.
func NewVAClient() (*VAClient, error) {
var u *url.URL
var err error
if os.Getenv("VCLOUDAIR_ENDPOINT") != "" {
u, err = url.ParseRequestURI(os.Getenv("VCLOUDAIR_ENDPOINT"))
if err != nil {
return &VAClient{}, fmt.Errorf("cannot parse endpoint coming from VCLOUDAIR_ENDPOINT")
}
} else {
// Implicitly trust this URL parse.
u, _ = url.ParseRequestURI("https://vchs.vmware.com/api")
}
VAClient := VAClient{
VAEndpoint: *u,
Client: Client{
APIVersion: "5.6",
// Patching things up as we're hitting several TLS timeouts.
Http: http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: 120 * time.Second,
},
},
},
}
return &VAClient, nil
}
示例2: ResolveChartRef
// ResolveChartRef resolves a chart reference to a URL.
//
// A reference may be an HTTP URL, a 'reponame/chartname' reference, or a local path.
func (c *ChartDownloader) ResolveChartRef(ref string) (*url.URL, error) {
// See if it's already a full URL.
u, err := url.ParseRequestURI(ref)
if err == nil {
// If it has a scheme and host and path, it's a full URL
if u.IsAbs() && len(u.Host) > 0 && len(u.Path) > 0 {
return u, nil
}
return u, fmt.Errorf("Invalid chart url format: %s", ref)
}
r, err := repo.LoadRepositoriesFile(c.HelmHome.RepositoryFile())
if err != nil {
return u, err
}
// See if it's of the form: repo/path_to_chart
p := strings.Split(ref, "/")
if len(p) > 1 {
if baseURL, ok := r.Repositories[p[0]]; ok {
if !strings.HasSuffix(baseURL, "/") {
baseURL = baseURL + "/"
}
return url.ParseRequestURI(baseURL + strings.Join(p[1:], "/"))
}
return u, fmt.Errorf("No such repo: %s", p[0])
}
return u, fmt.Errorf("Invalid chart url format: %s", ref)
}
示例3: ReadHandshake
func (c *hixie75ServerHandshaker) ReadHandshake(buf *bufio.Reader, req *http.Request) (code int, err error) {
c.Version = ProtocolVersionHixie75
if req.Method != "GET" || req.Proto != "HTTP/1.1" {
return http.StatusMethodNotAllowed, ErrBadRequestMethod
}
if req.Header.Get("Upgrade") != "WebSocket" {
return http.StatusBadRequest, ErrNotWebSocket
}
if req.Header.Get("Connection") != "Upgrade" {
return http.StatusBadRequest, ErrNotWebSocket
}
c.Origin, err = url.ParseRequestURI(strings.TrimSpace(req.Header.Get("Origin")))
if err != nil {
return http.StatusBadRequest, err
}
var scheme string
if req.TLS != nil {
scheme = "wss"
} else {
scheme = "ws"
}
c.Location, err = url.ParseRequestURI(scheme + "://" + req.Host + req.URL.RequestURI())
if err != nil {
return http.StatusBadRequest, err
}
protocol := strings.TrimSpace(req.Header.Get("Websocket-Protocol"))
protocols := strings.Split(protocol, ",")
for i := 0; i < len(protocols); i++ {
c.Protocol = append(c.Protocol, strings.TrimSpace(protocols[i]))
}
return http.StatusSwitchingProtocols, nil
}
示例4: TestSocketSameOrigin
func TestSocketSameOrigin(t *testing.T) {
tests := []struct {
name string
origins [2]string
expected bool
}{
{"Matching domains", [2]string{"https://example.com", "https://example.com"}, true},
{"Mismatched protocols", [2]string{"http://example.com", "https://example.com"}, false},
{"Mismatched domains", [2]string{"https://example.com", "https://example.org"}, false},
{"Mismatched ports", [2]string{"https://example.com:1", "https://example.com:2"}, false},
// This contradicts RFC 6454, but avoids complicating isSameOrigin with
// logic for validating and extracting port numbers from hosts.
// net.SplitHostPort is almost adequate for this, but conflates invalid
// hosts and missing ports.
{"Explicit ports", [2]string{"https://example.com", "https://example.com:443"}, false},
}
for _, test := range tests {
a, err := url.ParseRequestURI(test.origins[0])
if err != nil {
t.Errorf("On test %s, invalid URL %q: %s", test.name, test.origins[0], err)
}
b, err := url.ParseRequestURI(test.origins[1])
if err != nil {
t.Errorf("On test %s, invalid URL %q: %s", test.name, test.origins[1], err)
}
if actual := isSameOrigin(a, b); actual != test.expected {
t.Errorf("On test %s, got %s; want %s", test.name, actual, test.expected)
}
if actual := isSameOrigin(b, a); actual != test.expected {
t.Errorf("Test %s not transitive: got %s; want %s", test.name, actual,
test.expected)
}
}
}
示例5: TestDropRequestParam
func TestDropRequestParam(t *testing.T) {
// Without other params.
abc := `a.b.c:{"foo":[1,2,3,4]}`
abcEncoded := url.QueryEscape(abc)
uri, err := url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?request=%v`, abcEncoded))
if err != nil {
panic(err)
}
result := dropRequestParam(uri)
expected := "/v1/data/foo/bar"
if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
// With other params.
def := `d.e.f:{"bar":{"baz":null}}`
defEncoded := url.QueryEscape(def)
uri, err = url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?request=%v&pretty=true&depth=1&request=%v`, abcEncoded, defEncoded))
if err != nil {
panic(err)
}
result = dropRequestParam(uri)
expected = "/v1/data/foo/bar?depth=1&pretty=true"
if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}
示例6: TestClient_vaacquirecompute
func TestClient_vaacquirecompute(t *testing.T) {
cc := new(callCounter)
serv := httptest.NewServer(testHandler(map[string]testResponse{
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": {200, nil, vacompute},
}, cc))
// Set up a working client
os.Setenv("VCLOUDAIR_ENDPOINT", serv.URL+"/api")
client, err := NewClient()
if !assert.NoError(t, err) {
return
}
client.VAToken = "012345678901234567890123456789"
client.Region = "US - Anywhere"
auc, _ := url.ParseRequestURI(serv.URL + "/api/vchs/compute/00000000-0000-0000-0000-000000000000")
vavdchref, err := client.vaacquirecompute(auc, "VDC12345-6789")
if assert.NoError(t, err) && assert.Equal(t, 1, cc.Pop()) {
assert.Equal(t, serv.URL+"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession", vavdchref.String())
}
// Test client errors
testError := func(param string, resp testResponse) bool {
serv = httptest.NewServer(testHandler(map[string]testResponse{
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": resp,
}, cc))
os.Setenv("VCLOUDAIR_ENDPOINT", serv.URL+"/api")
client, err := NewClient()
if !assert.NoError(t, err) {
return false
}
client.VAToken = "012345678901234567890123456789"
client.Region = "US - Anywhere"
auc, _ := url.ParseRequestURI(serv.URL + "/api/vchs/compute/00000000-0000-0000-0000-000000000000")
_, err = client.vaacquirecompute(auc, param)
return assert.Error(t, err)
}
// Test a 404
if !testError("VDC12345-6789", testResponse{404, nil, notfoundErr}) {
return
}
// Test an API error
if !testError("VDC12345-6789", testResponse{500, nil, vcdError}) {
return
}
// Test an unknown VDC ID
if !testError("INVALID-6789", testResponse{200, nil, vacompute}) {
return
}
// Test an un-parsable response
if !testError("VDC12345-6789", testResponse{200, nil, notfoundErr}) {
return
}
}
示例7: Execute
func (cp *cpOpts) Execute(args []string) (err error) {
k, err := getAWSKeys()
if err != nil {
return
}
conf := new(s3gof3r.Config)
*conf = *s3gof3r.DefaultConfig
s3 := s3gof3r.New(cp.EndPoint, k)
conf.Concurrency = cp.Concurrency
if cp.NoSSL {
conf.Scheme = "http"
}
conf.PartSize = cp.PartSize
conf.Md5Check = !cp.NoMd5
conf.NTry = cp.NTry
s3gof3r.SetLogger(os.Stderr, "", log.LstdFlags, cp.Debug)
src, err := func(src string) (io.ReadCloser, error) {
if !strings.HasPrefix(strings.ToLower(src), "s3") {
return os.Open(src)
}
u, err := url.ParseRequestURI(src)
if err != nil {
return nil, fmt.Errorf("parse error: %s", err)
}
r, _, err := s3.Bucket(u.Host).GetReader(u.Path, conf)
return r, err
}(cp.Source)
if err != nil {
return
}
defer checkClose(src, err)
dst, err := func(dst string) (io.WriteCloser, error) {
if !strings.HasPrefix(strings.ToLower(dst), "s3") {
return os.Create(dst)
}
u, err := url.ParseRequestURI(dst)
if err != nil {
return nil, fmt.Errorf("parse error: %s", err)
}
return s3.Bucket(u.Host).PutWriter(u.Path, ACL(cp.Header, cp.ACL), conf)
}(cp.Dest)
if err != nil {
return
}
defer checkClose(dst, err)
_, err = io.Copy(dst, src)
return
}
示例8: TestRoute
func TestRoute(t *testing.T) {
payload1 := []byte("host one")
s1 := startTestServer(payload1, 0, voidCheck)
defer s1.Close()
payload2 := []byte("host two")
s2 := startTestServer(payload2, 0, voidCheck)
defer s2.Close()
doc := fmt.Sprintf(`
route1: Path("/host-one/*any") -> "%s";
route2: Path("/host-two/*any") -> "%s"
`, s1.URL, s2.URL)
dc, err := testdataclient.NewDoc(doc)
if err != nil {
t.Error(err)
}
p := New(routing.New(routing.Options{
nil,
routing.MatchingOptionsNone,
sourcePollTimeout,
[]routing.DataClient{dc},
nil,
0}), OptionsNone)
delay()
var (
r *http.Request
w *httptest.ResponseRecorder
u *url.URL
)
u, _ = url.ParseRequestURI("https://www.example.org/host-one/some/path")
r = &http.Request{
URL: u,
Method: "GET"}
w = httptest.NewRecorder()
p.ServeHTTP(w, r)
if w.Code != http.StatusOK || !bytes.Equal(w.Body.Bytes(), payload1) {
t.Error("wrong routing 1")
}
u, _ = url.ParseRequestURI("https://www.example.org/host-two/some/path")
r = &http.Request{
URL: u,
Method: "GET"}
w = httptest.NewRecorder()
p.ServeHTTP(w, r)
if w.Code != http.StatusOK || !bytes.Equal(w.Body.Bytes(), payload2) {
t.Error("wrong routing 2")
}
}
示例9: ReadHandshake
func (c *hybiServerHandshaker) ReadHandshake(buf *bufio.Reader, req *http.Request) (code int, err error) {
c.Version = ProtocolVersionHybi13
if req.Method != "GET" {
return http.StatusMethodNotAllowed, ErrBadRequestMethod
}
// HTTP version can be safely ignored.
if strings.ToLower(req.Header.Get("Upgrade")) != "websocket" ||
!strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade") {
return http.StatusBadRequest, ErrNotWebSocket
}
key := req.Header.Get("Sec-Websocket-Key")
if key == "" {
return http.StatusBadRequest, ErrChallengeResponse
}
version := req.Header.Get("Sec-Websocket-Version")
var origin string
switch version {
case "13":
c.Version = ProtocolVersionHybi13
origin = req.Header.Get("Origin")
case "8":
c.Version = ProtocolVersionHybi08
origin = req.Header.Get("Sec-Websocket-Origin")
default:
return http.StatusBadRequest, ErrBadWebSocketVersion
}
c.Origin, err = url.ParseRequestURI(origin)
if err != nil {
return http.StatusForbidden, err
}
var scheme string
if req.TLS != nil {
scheme = "wss"
} else {
scheme = "ws"
}
c.Location, err = url.ParseRequestURI(scheme + "://" + req.Host + req.URL.RequestURI())
if err != nil {
return http.StatusBadRequest, err
}
protocol := strings.TrimSpace(req.Header.Get("Sec-Websocket-Protocol"))
protocols := strings.Split(protocol, ",")
for i := 0; i < len(protocols); i++ {
c.Protocol = append(c.Protocol, strings.TrimSpace(protocols[i]))
}
c.accept, err = getNonceAccept([]byte(key))
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusSwitchingProtocols, nil
}
示例10: NewConfig
// NewConfig creates a new WebSocket config for client connection.
func NewConfig(server, origin string) (config *Config, err error) {
config = new(Config)
config.Version = ProtocolVersionHybi13
config.Location, err = url.ParseRequestURI(server)
if err != nil {
return
}
config.Origin, err = url.ParseRequestURI(origin)
if err != nil {
return
}
return
}
示例11: Configure
func (h *WebhookNotifier) Configure(config *config.NotifierConfig) (bool, error) {
// Get configuration
var httpConfig WebhookNotifierConfiguration
if config == nil {
return false, nil
}
if _, ok := config.Params["http"]; !ok {
return false, nil
}
yamlConfig, err := yaml.Marshal(config.Params["http"])
if err != nil {
return false, errors.New("invalid configuration")
}
err = yaml.Unmarshal(yamlConfig, &httpConfig)
if err != nil {
return false, errors.New("invalid configuration")
}
// Validate endpoint URL.
if httpConfig.Endpoint == "" {
return false, nil
}
if _, err := url.ParseRequestURI(httpConfig.Endpoint); err != nil {
return false, fmt.Errorf("could not parse endpoint URL: %s\n", err)
}
h.endpoint = httpConfig.Endpoint
// Setup HTTP client.
transport := &http.Transport{}
h.client = &http.Client{
Transport: transport,
Timeout: timeout,
}
// Initialize TLS.
transport.TLSClientConfig, err = loadTLSClientConfig(&httpConfig)
if err != nil {
return false, fmt.Errorf("could not initialize client cert auth: %s\n", err)
}
// Set proxy.
if httpConfig.Proxy != "" {
proxyURL, err := url.ParseRequestURI(httpConfig.Proxy)
if err != nil {
return false, fmt.Errorf("could not parse proxy URL: %s\n", err)
}
transport.Proxy = http.ProxyURL(proxyURL)
}
return true, nil
}
示例12: FindEdgeGateway
func (v *Vdc) FindEdgeGateway(edgegateway string) (EdgeGateway, error) {
for _, av := range v.Vdc.Link {
if av.Rel == "edgeGateways" && av.Type == "application/vnd.vmware.vcloud.query.records+xml" {
u, err := url.ParseRequestURI(av.HREF)
if err != nil {
return EdgeGateway{}, fmt.Errorf("error decoding vdc response: %s", err)
}
// Querying the Result list
req := v.c.NewRequest(map[string]string{}, "GET", *u, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return EdgeGateway{}, fmt.Errorf("error retrieving edge gateway records: %s", err)
}
query := new(types.QueryResultEdgeGatewayRecordsType)
if err = decodeBody(resp, query); err != nil {
return EdgeGateway{}, fmt.Errorf("error decoding edge gateway query response: %s", err)
}
u, err = url.ParseRequestURI(query.EdgeGatewayRecord.HREF)
if err != nil {
return EdgeGateway{}, fmt.Errorf("error decoding edge gateway query response: %s", err)
}
// Querying the Result list
req = v.c.NewRequest(map[string]string{}, "GET", *u, nil)
resp, err = checkResp(v.c.Http.Do(req))
if err != nil {
return EdgeGateway{}, fmt.Errorf("error retrieving edge gateway: %s", err)
}
edge := NewEdgeGateway(v.c)
if err = decodeBody(resp, edge.EdgeGateway); err != nil {
return EdgeGateway{}, fmt.Errorf("error decoding edge gateway response: %s", err)
}
return *edge, nil
}
}
return EdgeGateway{}, fmt.Errorf("can't find Edge Gateway")
}
示例13: TestParseAndDispatchRawData
func TestParseAndDispatchRawData(t *testing.T) {
url1 := "https://www.zalando.de"
data := `hello: Path("/hello") -> "https://www.zalando.de"`
dc := mock.MakeDataClient(data)
mwr := &mock.FilterRegistry{}
d := dispatch.Make()
s := MakeSource(dc, mwr, d)
c1 := make(chan skipper.Settings)
c2 := make(chan skipper.Settings)
s.Subscribe(c1)
s.Subscribe(c2)
r, _ := http.NewRequest("GET", "http://localhost:9090/hello", nil)
// let the settings be populated:
time.Sleep(3 * time.Millisecond)
s1 := <-c1
s2 := <-c2
rt1, _ := s1.Route(r)
rt2, _ := s2.Route(r)
up1, _ := url.ParseRequestURI(url1)
if rt1.Backend().Scheme() != up1.Scheme || rt1.Backend().Host() != up1.Host ||
rt2.Backend().Scheme() != up1.Scheme || rt2.Backend().Host() != up1.Host {
t.Error("wrong url 1")
}
data = `hello: Path("/hello") -> "https://www.zalan.do"`
dc.Feed(data)
// let the new settings fan through
time.Sleep(3 * time.Millisecond)
s1 = <-c1
s2 = <-c2
rt1, _ = s1.Route(r)
rt2, _ = s2.Route(r)
up2, _ := url.ParseRequestURI("https://www.zalan.do")
if rt1.Backend().Scheme() != up2.Scheme || rt1.Backend().Host() != up2.Host ||
rt2.Backend().Scheme() != up2.Scheme || rt2.Backend().Host() != up2.Host {
t.Error("wrong url 2")
}
}
示例14: NewConfig
// NewConfig creates a new WebSocket config for client connection.
func NewConfig(server, origin, host string) (config *Config, err error) {
config = new(Config)
config.Version = ProtocolVersionHybi13
config.Location, err = url.ParseRequestURI(server)
if err != nil {
return
}
config.Origin, err = url.ParseRequestURI(origin)
if err != nil {
return
}
config.Header = http.Header(make(map[string][]string))
config.HostOverride = host
return
}
示例15: TestStartHandleStop
func (s *ServerSuite) TestStartHandleStop() {
// Start
taskHandler := func(a *acomm.Request) (interface{}, *url.URL, error) {
return nil, nil, nil
}
s.server.RegisterTask("foobar", taskHandler)
if !s.NoError(s.server.Start(), "failed to start server") {
return
}
time.Sleep(time.Second)
// Stop
defer s.server.Stop()
// Handle request
tracker := s.server.Tracker()
handled := make(chan struct{})
respHandler := func(req *acomm.Request, resp *acomm.Response) {
close(handled)
}
req, _ := acomm.NewRequest("foobar", tracker.URL().String(), struct{}{}, respHandler, respHandler)
providerSocket, _ := url.ParseRequestURI("unix://" + s.server.TaskSocketPath("foobar"))
if !s.NoError(s.server.Tracker().TrackRequest(req, 5*time.Second)) {
return
}
if !s.NoError(acomm.Send(providerSocket, req)) {
return
}
<-handled
}