<commit_before>package f

import(
    "regexp"
    "strings"
    "net/url"
    "encoding/json"
    "github.com/ricallinson/stackr"
    "github.com/ricallinson/httphelp"
)

/*
    A Request represents an HTTP request received by the server.
*/
type Request struct {

    // The stackr.Request type.
    *stackr.Request

    // The forgery.Response matched to this request.
    res *Response

    // The application server.
    app *Server

    // Return the remote address, or when "trust proxy" is enabled - the upstream address.
    Ip string

    // When "trust proxy" is `true`, parse the "X-Forwarded-For" ip address list and return a slice, 
    // otherwise an empty slice is returned. For example if the value were "client, proxy1, proxy2" 
    // you would receive the slice {"client", "proxy1", "proxy2"} where "proxy2" is the furthest down-stream.
    Ips []string

    // Returns the request URL pathname.
    Path string

    // Check if the request was issued with the "X-Requested-With" header field set to "XMLHttpRequest" (jQuery etc).
    Xhr bool

    // Return the protocol string "http" or "https" when requested with TLS. 
    // When the "trust proxy" setting is enabled the "X-Forwarded-Proto" header field will be trusted. 
    // If you're running behind a reverse proxy that supplies https for you this may be enabled.
    Protocol string

    // Check if a TLS connection is established. This is a short-hand for: "https" == req.Protocol
    Secure bool

    // This property is a slice containing properties mapped to the named route "parameters". 
    // For example if you have the route "/user/:name", then the "name" property is available 
    // to you as req.params["name"]. This object defaults to {}.
    Params map[string]string

    // The currently matched Route containing several properties such as the 
    // route's original path string, the regexp generated, and so on.
    Route interface{}

    //
    accepted []string

    //
    acceptedLanguages []string

    //
    acceptedCharsets []string
}

/*
    Returns a new Request.
*/

func createRequest(req *stackr.Request, app *Server) (*Request) {

    this := &Request{}

    this.Request = req

    this.app = app

    if t, v := this.app.Get("trust proxy"), this.Header.Get("X-Forwarded-For"); len(t) > 0 && len(v) > 0 {
        s := regexp.MustCompile(" *, *").Split(v, -1)
        this.Ip = s[0]
        this.Ips = s
    } else {
        this.Ip = this.RemoteAddr
        this.Ips = []string{}
    }

    // Helpers for standard headers.
    this.Path = this.URL.Path
    this.Xhr = this.Header.Get("X-Requested-With") == "XMLHttpRequest"
    this.Protocol = this.URL.Scheme
    this.Secure = this.Protocol == "https"

    this.Params = map[string]string{}

    // Could have been set by middleware.
    if this.Body == nil {
        this.Body = map[string]string{}
    }

    // Could have been set by middleware.
    if this.Query == nil {
        this.Query = map[string]string{}
    }

    // Could have been set by middleware.
    if this.Files == nil {
        this.Files = map[string]interface{}{}
    }

    return this
}

/*
    Contains the cookies sent by the user-agent.
*/
func (this *Request) Cookie(n string, i ...interface{}) (string) {
    
    cookie, err := this.Request.Cookie(n)

    if err != nil {
        return ""
    }

    /*
        Unescape the cookie.
    */
    var v string
    var e error
    v, e = url.QueryUnescape(cookie.Value)
    v, e = Decode(v)

    if e != nil {
        return ""
    }

    /*
        If no interface was given then just return the Value.
    */

    if len(i) == 0 {
        return v
    }

    json.Unmarshal([]byte(v), i[0])

    return v
}

/*
    Contains the signed cookies sent by the user-agent, unsigned and ready for use. 
    Signed cookies are accessed by a different function to show developer intent, otherwise a 
    malicious attack could be placed on `req.Cookie` values which are easy to spoof. 
    Note that signing a cookie does not mean it is "hidden" nor encrypted, this simply 
    prevents tampering as the secret used to sign is private.
*/
func (this *Request) SignedCookie(n string, i ...interface{}) (string) {

    secret := this.app.Get("secret")

    if secret == "" {
        panic("f.Get(\"secret\") required for signed cookies")
    }

    v := this.Cookie(n)

    /*
        Decrypt the cookie string value.
    */

    v = Unsign(v, secret)

    /*
        If no interface was given then just return the Value.
    */

    if len(i) == 0 {
        return v
    }

    json.Unmarshal([]byte(v), i[0])

    return v
}

/*
    Return the value of param "name" when present. Lookup is performed in the following order:

    * Params
    * Body
    * Query

    Direct access to req.body, req.params, and req.query should be favoured for clarity - 
    unless you truly accept input from each object.
*/
func (this *Request) Param(n string) (string) {<commit_msg>Hooked up URL query params to req.Param() function<commit_after>package f

import(
    "regexp"
    "strings"
    "net/url"
    "encoding/json"
    "github.com/ricallinson/stackr"
    "github.com/ricallinson/httphelp"
)

/*
    A Request represents an HTTP request received by the server.
*/
type Request struct {

    // The stackr.Request type.
    *stackr.Request

    // The forgery.Response matched to this request.
    res *Response

    // The application server.
    app *Server

    // Return the remote address, or when "trust proxy" is enabled - the upstream address.
    Ip string

    // When "trust proxy" is `true`, parse the "X-Forwarded-For" ip address list and return a slice, 
    // otherwise an empty slice is returned. For example if the value were "client, proxy1, proxy2" 
    // you would receive the slice {"client", "proxy1", "proxy2"} where "proxy2" is the furthest down-stream.
    Ips []string

    // Returns the request URL pathname.
    Path string

    // Check if the request was issued with the "X-Requested-With" header field set to "XMLHttpRequest" (jQuery etc).
    Xhr bool

    // Return the protocol string "http" or "https" when requested with TLS. 
    // When the "trust proxy" setting is enabled the "X-Forwarded-Proto" header field will be trusted. 
    // If you're running behind a reverse proxy that supplies https for you this may be enabled.
    Protocol string

    // Check if a TLS connection is established. This is a short-hand for: "https" == req.Protocol
    Secure bool

    // This property is a slice containing properties mapped to the named route "parameters". 
    // For example if you have the route "/user/:name", then the "name" property is available 
    // to you as req.params["name"]. This object defaults to {}.
    Params map[string]string

    // The currently matched Route containing several properties such as the 
    // route's original path string, the regexp generated, and so on.
    Route interface{}

    //
    accepted []string

    //
    acceptedLanguages []string

    //
    acceptedCharsets []string
}

/*
    Returns a new Request.
*/

func createRequest(req *stackr.Request, app *Server) (*Request) {

    this := &Request{}

    this.Request = req

    this.app = app

    if t, v := this.app.Get("trust proxy"), this.Header.Get("X-Forwarded-For"); len(t) > 0 && len(v) > 0 {
        s := regexp.MustCompile(" *, *").Split(v, -1)
        this.Ip = s[0]
        this.Ips = s
    } else {
        this.Ip = this.RemoteAddr
        this.Ips = []string{}
    }

    // Helpers for standard headers.
    this.Path = this.URL.Path
    this.Xhr = this.Header.Get("X-Requested-With") == "XMLHttpRequest"
    this.Protocol = this.URL.Scheme
    this.Secure = this.Protocol == "https"

    this.Params = map[string]string{}

    // Could have been set by middleware.
    if this.Body == nil {
        // Is there a standard body parser in Go that can be used here?
        // b, err := ioutil.ReadAll(this.Request.Request.Body);
        this.Body = map[string]string{}
    }

    // Could have been set by middleware.
    if this.Query == nil {
        this.Query = map[string]string{}
        // Copy the first item for each key from this.Request.URL.Query() into this.Query
        // This has a performance impact so is it worth it?
        // The alternative is to access this.URL.Query() from this.Param() like so;
        //     v, ok = this.URL.Query().Get(n)
        for k, v := range this.URL.Query() {
            this.Query[k] = v[0]
        }
    }

    // Could have been set by middleware.
    if this.Files == nil {
        this.Files = map[string]interface{}{}
    }

    return this
}

/*
    Contains the cookies sent by the user-agent.
*/
func (this *Request) Cookie(n string, i ...interface{}) (string) {
    
    cookie, err := this.Request.Cookie(n)

    if err != nil {
        return ""
    }

    /*
        Unescape the cookie.
    */
    var v string
    var e error
    v, e = url.QueryUnescape(cookie.Value)
    v, e = Decode(v)

    if e != nil {
        return ""
    }

    /*
        If no interface was given then just return the Value.
    */

    if len(i) == 0 {
        return v
    }

    json.Unmarshal([]byte(v), i[0])

    return v
}

/*
    Contains the signed cookies sent by the user-agent, unsigned and ready for use. 
    Signed cookies are accessed by a different function to show developer intent, otherwise a 
    malicious attack could be placed on `req.Cookie` values which are easy to spoof. 
    Note that signing a cookie does not mean it is "hidden" nor encrypted, this simply 
    prevents tampering as the secret used to sign is private.
*/
func (this *Request) SignedCookie(n string, i ...interface{}) (string) {

    secret := this.app.Get("secret")

    if secret == "" {
        panic("f.Get(\"secret\") required for signed cookies")
    }

    v := this.Cookie(n)

    /*
        Decrypt the cookie string value.
    */

    v = Unsign(v, secret)

    /*
        If no interface was given then just return the Value.
    */

    if len(i) == 0 {
        return v
    }

    json.Unmarshal([]byte(v), i[0])

    return v
}

/*
    Return the value of param "name" when present. Lookup is performed in the following order:

    * Params
    * Body
    * Query

    Direct access to req.body, req.params, and req.query should be favored for clarity - 
    unless you truly accept input from each object.
*/
func (this *Request) Param(n string) (string) {
{'fraction_non_alphanumeric': 0.11062243044268094, 'fraction_numerical': 0.002581508748446314, 'mean_word_length': 2.3048973143759874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3977755', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3030, 'n_words': 1172}