まずは上手く取れた記述から

 1package controllers
 2
 3import (
 4    "github.com/revel/revel"
 5)
 6
 7type Mycontroller struct {
 8    *revel.Controller
 9}
10
11
12func (m *Mycontroller) Index() revel.Result {
13    // redirectURLの取得
14    requestedHost := m.Request.Host
15
16    requestedScheme := "http"
17    if m.Request.TLS != nil {
18        requestedScheme = "https"
19    }
20
21    // ホスト名
22    revel.TRACE.Println(requestedHost);
23
24    // おまけにスキームも判断してみる(httpかhttpsか)
25    revel.TRACE.Println(requestedScheme);
26}

これで取れる理由

Controllerが継承している構造体を見ていくと、

[https://github.com/revel/revel/blob/master/controller.go:embed:cite]

 1type Controller struct {
 2	Name          string          // The controller name, e.g. "Application"
 3	Type          *ControllerType // A description of the controller type.
 4	MethodName    string          // The method name, e.g. "Index"
 5	MethodType    *MethodType     // A description of the invoked action type.
 6	AppController interface{}     // The controller that was instantiated.
 7	Action        string          // The fully qualified action name, e.g. "App.Index"
 8
 9	Request  *Request
10	Response *Response
11	Result   Result
12
13	Flash      Flash                  // User cookie, cleared after 1 request.
14	Session    Session                // Session, stored in cookie, signed.
15	Params     *Params                // Parameters from URL and form (including multipart).
16	Args       map[string]interface{} // Per-request scratch space.
17	RenderArgs map[string]interface{} // Args passed to the template.
18	Validation *Validation            // Data validation helpers
19}

Requestを継承している事がわかる。

Requestをもう少し見てみよう。

[https://golang.org/pkg/net/http/#Request:title]

 1type Request struct {
 2
 3        // 〜中略〜
 4
 5        // URL specifies either the URI being requested (for server
 6        // requests) or the URL to access (for client requests).
 7        //
 8        // For server requests the URL is parsed from the URI
 9        // supplied on the Request-Line as stored in RequestURI.  For
10        // most requests, fields other than Path and RawQuery will be
11        // empty. (See RFC 2616, Section 5.1.2)
12        //
13        // For client requests, the URL's Host specifies the server to
14        // connect to, while the Request's Host field optionally
15        // specifies the Host header value to send in the HTTP
16        // request.
17        URL *url.URL
18
19        // 〜中略〜
20
21        // For server requests Host specifies the host on which the
22        // URL is sought. Per RFC 2616, this is either the value of
23        // the "Host" header or the host name given in the URL itself.
24        // It may be of the form "host:port".
25        //
26        // For client requests Host optionally overrides the Host
27        // header to send. If empty, the Request.Write method uses
28        // the value of URL.Host.
29        Host string
30
31        // 〜中略〜
32
33        // RequestURI is the unmodified Request-URI of the
34        // Request-Line (RFC 2616, Section 5.1) as sent by the client
35        // to a server. Usually the URL field should be used instead.
36        // It is an error to set this field in an HTTP client request.
37        RequestURI string
38
39        // 〜中略〜
40
41        // TLS allows HTTP servers and other software to record
42        // information about the TLS connection on which the request
43        // was received. This field is not filled in by ReadRequest.
44        // The HTTP server in this package sets the field for
45        // TLS-enabled connections before invoking a handler;
46        // otherwise it leaves the field nil.
47        // This field is ignored by the HTTP client.
48        TLS *tls.ConnectionState
49
50        // 〜中略〜
51
52}

Requestの中で、この3つが使えそうだ。

  • URL *url.URL
  • Host string
  • TLS *tls.ConnectionState

URL

まずはこれが良さそうなので、a.Request.URL.Hostとa.Request.URL.Schemeを取ってみるも、空の文字列が返ってくる。

理由はコメントに書いてあった。最近すこぶる性能が良くなったGoogle翻訳にコメント部分を突っ込んでみる

For server requests the URL is parsed from the URI supplied on the Request-Line as stored in RequestURI.

サーバ要求の場合、URLはRequestURIに格納されているRequest-Lineで指定されたURIから解析されます。

ということで、RequestURIに格納されている文字列をParseしたものなので、そもそもRequestURIにはホスト名が含まれていなかったので得ることができなかった。

Host

これはホスト名を得ることが出来た。port番号が指定されていればportつきで取得できる。

TLS

上記でホスト名取得ができたがこちらも見ておく。

The HTTP server in this package sets the field for TLS-enabled connections before invoking a handler; otherwise it leaves the field nil.

このパッケージのHTTPサーバーは、ハンドラを呼び出す前にTLS対応接続のフィールドを設定します。 それ以外の場合は、フィールドはnilになります。

ほほう。TLSでの接続じゃない場合、nilになるとのこと。ということでここのnilチェックでhttpかhttpsが判別できそう。

ということで、ブログ冒頭のようにホスト名取得(とおまけにhttp/httpsの判別)ができた。