(Golang) Compound Problems with HTTP When Attempting Basic Authentication with Go

260 views
Skip to first unread message

Revence Kalibwani

unread,
Sep 3, 2012, 11:16:14 AM9/3/12
to google-a...@googlegroups.com
This little piece of code shows three problems I have encountered (and solved, with varying levels of success) while using Go on App Engine.

  endpoint, e :=  url.Parse(streamType)
  if e != nil { return stream, t.New("Parsing for %s failed.", streamType) }
  //  endpoint.User = url.UserPassword(auth.Username, auth.Password)
  client.Transport  = &http.Transport {
    Proxy : func(req *http.Request) (*url.URL, error) {
      req.SetBasicAuth(auth.Username, auth.Password)
      return nil, nil
    },
  }
  if post {
    resp, e = client.PostForm(endpoint.String(), options)
  } else {
    endpoint.RawQuery = options.Encode()
    resp, e = client.Get(endpoint.String())
  }
  if e != nil { return stream, t.New("Could not fetch %s (%s): %v", endpoint, options.Encode(), e) }


While URL’s that contain auth information are valid—like http://user:pass...@www.1st.ug/ for example—the parser in Go’s net/url package can’t handle them. (In fact, in the code, the logic for dealing with URLs that have auth information is there, but the parsing of them fails consistently miserably.) This is the case both in development and in production.
My work-around, as you can see, was first to try to set the User attribute of the URL object. But for POST requests, it is ignored. Since I will be dealing with POST requests (as well as GET), I chose the option below it. I created a fake proxy, whose only job was to enforce auth. It does it on the Request object, which is never exposed anywhere at any point by the API. The trick works.
Then another problem shows up, in that in development, the thing works flawlessly, but in production it consistently returns permission denied. This is provoked by the proxy—in that GAE doesn’t allow me to work the proxy, even though that restriction is not documented explicity. When I remove the proxy dance, it works.

I wonder how one can express Basic Authentication for a HTTP request without running afoul of the gae gopher.
(This discussion system allows me to start a discussion xor ask a question; I want to do both!)

Johan Euphrosine

unread,
Sep 3, 2012, 11:25:10 AM9/3/12
to google-a...@googlegroups.com
Hi Revence,

Have you already tried to ask the same question on Stack Overflow or
on google-appengine-go@ (they are still using a separate group while
the runtime is experimental):
http://stackoverflow.com/questions/tagged/google-app-engine

When defining a custom transport on App Engine you should make sure
you use urlfetch.Transport to make the actual request:
http://code.google.com/p/appengine-go/source/browse/appengine/urlfetch/urlfetch.go#29

Hope that helps.
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-appengine/-/MrznnN5m970J.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengi...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.



--
Johan Euphrosine (proppy)
Developer Programs Engineer
Google Developer Relations

Revence Kalibwani

unread,
Sep 3, 2012, 11:42:20 AM9/3/12
to google-a...@googlegroups.com
Sorry for that.

I was using a separate Transport. I found the solution.
I’ve done roughly what is here:
Line 308.
For some reason, I always though that it was untenable, thinking that it would require more than just the querystring as the Reader.
Reply all
Reply to author
Forward
0 new messages