2015-01-01 18:02:25 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2014-06-24 04:47:43 +02:00
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2014-07-23 08:25:35 +02:00
|
|
|
// Package query implements a faster single-purpose URL Query parser.
|
2014-06-24 04:47:43 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2014-07-02 03:40:29 +02:00
|
|
|
"strings"
|
2014-06-24 04:47:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Query represents a parsed URL.Query.
|
|
|
|
type Query struct {
|
|
|
|
Infohashes []string
|
|
|
|
Params map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New parses a raw url query.
|
|
|
|
func New(query string) (*Query, error) {
|
|
|
|
var (
|
|
|
|
keyStart, keyEnd int
|
|
|
|
valStart, valEnd int
|
|
|
|
firstInfohash string
|
|
|
|
|
|
|
|
onKey = true
|
|
|
|
hasInfohash = false
|
|
|
|
|
|
|
|
q = &Query{
|
|
|
|
Infohashes: nil,
|
|
|
|
Params: make(map[string]string),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i, length := 0, len(query); i < length; i++ {
|
|
|
|
separator := query[i] == '&' || query[i] == ';' || query[i] == '?'
|
2015-06-18 16:34:52 +02:00
|
|
|
last := i == length-1
|
|
|
|
|
|
|
|
if separator || last {
|
|
|
|
if onKey && !last {
|
2014-06-24 04:47:43 +02:00
|
|
|
keyStart = i + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-18 16:34:52 +02:00
|
|
|
if last && !separator && !onKey {
|
2014-06-24 04:47:43 +02:00
|
|
|
valEnd = i
|
|
|
|
}
|
|
|
|
|
|
|
|
keyStr, err := url.QueryUnescape(query[keyStart : keyEnd+1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-18 16:34:52 +02:00
|
|
|
var valStr string
|
|
|
|
|
|
|
|
if valEnd > 0 {
|
|
|
|
valStr, err = url.QueryUnescape(query[valStart : valEnd+1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-24 04:47:43 +02:00
|
|
|
}
|
|
|
|
|
2014-07-02 03:40:29 +02:00
|
|
|
q.Params[strings.ToLower(keyStr)] = valStr
|
2014-06-24 04:47:43 +02:00
|
|
|
|
|
|
|
if keyStr == "info_hash" {
|
|
|
|
if hasInfohash {
|
|
|
|
// Multiple infohashes
|
|
|
|
if q.Infohashes == nil {
|
|
|
|
q.Infohashes = []string{firstInfohash}
|
|
|
|
}
|
|
|
|
q.Infohashes = append(q.Infohashes, valStr)
|
|
|
|
} else {
|
|
|
|
firstInfohash = valStr
|
|
|
|
hasInfohash = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 16:34:52 +02:00
|
|
|
valEnd = 0
|
2014-06-24 04:47:43 +02:00
|
|
|
onKey = true
|
|
|
|
keyStart = i + 1
|
|
|
|
|
|
|
|
} else if query[i] == '=' {
|
|
|
|
onKey = false
|
|
|
|
valStart = i + 1
|
2015-06-18 16:34:52 +02:00
|
|
|
valEnd = 0
|
2014-06-24 04:47:43 +02:00
|
|
|
} else if onKey {
|
|
|
|
keyEnd = i
|
|
|
|
} else {
|
|
|
|
valEnd = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return q, nil
|
|
|
|
}
|
|
|
|
|
2014-07-23 21:56:15 +02:00
|
|
|
// Uint64 is a helper to obtain a uint of any length from a Query. After being
|
|
|
|
// called, you can safely cast the uint64 to your desired length.
|
2014-06-24 04:47:43 +02:00
|
|
|
func (q *Query) Uint64(key string) (uint64, error) {
|
|
|
|
str, exists := q.Params[key]
|
|
|
|
if !exists {
|
|
|
|
return 0, errors.New("value does not exist for key: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.ParseUint(str, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|