2013-06-22 03:43:11 +02:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2013-06-22 01:31:32 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2013-06-22 03:43:11 +02:00
|
|
|
"net/url"
|
2013-06-22 01:31:32 +02:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2013-07-12 06:36:24 +02:00
|
|
|
// parsedQuery represents a parsed URL.Query.
|
2013-06-22 01:31:32 +02:00
|
|
|
type parsedQuery struct {
|
2013-07-12 06:36:24 +02:00
|
|
|
Infohashes []string
|
|
|
|
Params map[string]string
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 07:09:02 +02:00
|
|
|
// getUint64 is a helper to obtain a uint64 from a parsedQuery.
|
2013-07-12 06:36:24 +02:00
|
|
|
func (pq *parsedQuery) getUint64(key string) (uint64, error) {
|
|
|
|
str, exists := pq.Params[key]
|
2013-06-22 01:31:32 +02:00
|
|
|
if !exists {
|
2014-05-08 07:09:02 +02:00
|
|
|
return 0, errors.New("value does not exist for key: " + key)
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
val, err := strconv.ParseUint(str, 10, 64)
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
2013-07-12 06:36:24 +02:00
|
|
|
return 0, err
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-07-12 06:36:24 +02:00
|
|
|
return val, nil
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:36:24 +02:00
|
|
|
// parseQuery parses a raw url query.
|
2013-06-22 01:31:32 +02:00
|
|
|
func parseQuery(query string) (*parsedQuery, error) {
|
|
|
|
var (
|
|
|
|
keyStart, keyEnd int
|
|
|
|
valStart, valEnd int
|
|
|
|
firstInfohash string
|
|
|
|
|
|
|
|
onKey = true
|
|
|
|
hasInfohash = false
|
|
|
|
|
|
|
|
pq = &parsedQuery{
|
2013-07-12 06:36:24 +02:00
|
|
|
Infohashes: nil,
|
|
|
|
Params: make(map[string]string),
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i, length := 0, len(query); i < length; i++ {
|
|
|
|
separator := query[i] == '&' || query[i] == ';' || query[i] == '?'
|
|
|
|
if separator || i == length-1 {
|
|
|
|
if onKey {
|
|
|
|
keyStart = i + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == length-1 && !separator {
|
|
|
|
if query[i] == '=' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
valEnd = i
|
|
|
|
}
|
|
|
|
|
|
|
|
keyStr, err := url.QueryUnescape(query[keyStart : keyEnd+1])
|
|
|
|
if err != nil {
|
2013-06-22 03:43:11 +02:00
|
|
|
return nil, err
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-06-22 01:31:32 +02:00
|
|
|
valStr, err := url.QueryUnescape(query[valStart : valEnd+1])
|
|
|
|
if err != nil {
|
2013-06-22 03:43:11 +02:00
|
|
|
return nil, err
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:36:24 +02:00
|
|
|
pq.Params[keyStr] = valStr
|
2013-06-22 01:31:32 +02:00
|
|
|
|
|
|
|
if keyStr == "info_hash" {
|
|
|
|
if hasInfohash {
|
|
|
|
// Multiple infohashes
|
2013-07-12 06:36:24 +02:00
|
|
|
if pq.Infohashes == nil {
|
|
|
|
pq.Infohashes = []string{firstInfohash}
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2013-07-12 06:36:24 +02:00
|
|
|
pq.Infohashes = append(pq.Infohashes, valStr)
|
2013-06-22 01:31:32 +02:00
|
|
|
} else {
|
|
|
|
firstInfohash = valStr
|
|
|
|
hasInfohash = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onKey = true
|
|
|
|
keyStart = i + 1
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-06-22 01:31:32 +02:00
|
|
|
} else if query[i] == '=' {
|
|
|
|
onKey = false
|
|
|
|
valStart = i + 1
|
|
|
|
} else if onKey {
|
|
|
|
keyEnd = i
|
|
|
|
} else {
|
|
|
|
valEnd = i
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
return pq, nil
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|