Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/httputil: add sort by quality #3594

Closed
gopherbot opened this issue May 6, 2012 · 21 comments
Closed

net/http/httputil: add sort by quality #3594

gopherbot opened this issue May 6, 2012 · 21 comments
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@gopherbot
Copy link

by raul.san@sent.com:

There is an usefull method which could be added to the type Request in package
"net/http"

type Reverse struct {
    // This embedded Interface permits Reverse to use the methods of
    // another Interface implementation.
    sort.Interface
}

// Less returns the opposite of the embedded implementation's Less method.
func (r Reverse) Less(i, j int) bool {
    return r.Interface.Less(j, i)
}

// parseQualityValues returns the fields sorted by its greater quality level.
// Returns nil if there are no values associated with the header.
//
// [HTTP spec](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
// [Quality Values](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9)
func parseQualityValues(r *http.Request, header string) []string {
    valHeader := r.Header.Get(header)
    if len(valHeader) == 0 {
        return nil
    }

    qValues := make(map[float64][]string)
    qSorted := make([]float64, 0)
    result := make([]string, 0)

    // Example of string to parse for the header Accept-Language:
    //   da, en-gb;q=0.8, en;q=0.7
    for _, f := range strings.Split(valHeader, ",") {
        lang := ""
        q := 1.0 // default

        if strings.ContainsRune(f, ';') {
            q_ := strings.Split(f, "=")[1]
            q_ = strings.Trim(q_, " ")

            q, _ = strconv.ParseFloat(q_, 64)
            if q == 0 { // content not acceptable for the client
                continue
            }
            lang = strings.Split(f, ";")[0]
            lang = strings.Trim(lang, " ")
        } else {
            lang = strings.Trim(f, " ")
        }

        if _, ok := qValues[q]; !ok {
            qValues[q] = make([]string, 0)
        }
        qValues[q] = append(qValues[q], lang)
        qSorted = append(qSorted, q)
    }

    sort.Sort(Reverse{sort.Float64Slice(qSorted)})

    for _, v := range qSorted {
        result = append(result, qValues[v]...)
    }
    return result
}
@gopherbot
Copy link
Author

Comment 1 by raul.san@sent.com:

Note: substitute variable "lang" by another name like "key".

@bradfitz
Copy link
Contributor

bradfitz commented May 7, 2012

Comment 2:

Agreed. There was a CL containing this awhile back that didn't make Go 1. We should
probably add this at some point, either in http or httputil.

Owner changed to @bradfitz.

Status changed to Accepted.

@gopherbot
Copy link
Author

Comment 3 by raul.san@sent.com:

I've added a file with a bug fixed, and returning the parameters with zero value too
since the server could use it for statistic (which arguments want not the clients).

Attachments:

  1. foo.go (1561 bytes)

@gopherbot
Copy link
Author

Comment 4 by raul.san@sent.com:

I've added a file with a bug fixed, and returning the parameters with zero value too
since the server could use it for statistic (which arguments want not the clients).

Attachments:

  1. foo.go (1564 bytes)

@gopherbot
Copy link
Author

Comment 5 by raul.san@sent.com:

I've added a file with a bug fixed, and returning the parameters with zero value too
since the server could use it for statistic (which arguments want not the clients).

Attachments:

  1. foo.go (1560 bytes)

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 6:

Labels changed: added priority-later, removed priority-triage.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 7:

Labels changed: added go1.1.

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 8:

Labels changed: added size-m.

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 9:

Labels changed: added suggested.

@kisielk
Copy link
Contributor

kisielk commented Feb 10, 2013

Comment 10:

I'm interested in working on this since I have some code that implements some of this
already.
The code I have currently returns not only the value but also its quality as a float. Do
we want this? or just a sorted list of the values? It would mean you'd need a type
instead of getting a []string, but on the other hand you don't need two return values
because you could put the values with a 0 q in the same slice.

@bradfitz
Copy link
Contributor

Comment 11:

Kamil, sure, go for it.  Let's discuss an API first.  The code is less interesting.

@bradfitz
Copy link
Contributor

Comment 12:

No discussion of API happened in time.  This now misses Go 1.1.
Not that it matters much, though, since nothing in net/http itself really needs this
(that I know of), so it can be implemented in external packages for now.

Labels changed: removed go1.1.

Owner changed to ---.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 13:

Labels changed: added go1.2maybe.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 14:

Labels changed: added feature.

@robpike
Copy link
Contributor

robpike commented Aug 16, 2013

Comment 15:

Will not happen for Go 1.2

Labels changed: added go1.3maybe, removed go1.2maybe.

@robpike
Copy link
Contributor

robpike commented Aug 20, 2013

Comment 16:

Labels changed: removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 17:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 18:

Labels changed: removed feature.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 19:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 20:

Labels changed: added repo-main.

@gopherbot gopherbot added accepted Suggested Issues that may be good for new contributors looking for work to do. labels Dec 4, 2013
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@bradfitz
Copy link
Contributor

There seems to be lack of interest to add this, and/or lack of need, so I'm going to close this for now. I'm happy to entertain a patch if anybody cares enough later.

@golang golang locked and limited conversation to collaborators Sep 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

5 participants