2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-12-06 06:34:46 +00:00
|
|
|
package httputil
|
2010-07-26 03:34:04 +00:00
|
|
|
|
|
|
|
import (
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"encoding/json"
|
2010-07-26 03:34:04 +00:00
|
|
|
"fmt"
|
2010-12-02 06:34:08 +00:00
|
|
|
"log"
|
2012-09-16 22:20:49 +00:00
|
|
|
"net"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
2012-09-16 22:20:49 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2011-05-06 00:15:32 +00:00
|
|
|
"strings"
|
2012-09-16 22:20:49 +00:00
|
|
|
|
|
|
|
"camlistore.org/pkg/auth"
|
2010-07-26 03:34:04 +00:00
|
|
|
)
|
|
|
|
|
2011-05-31 17:20:28 +00:00
|
|
|
func ErrorRouting(conn http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Error(conn, "Handlers wired up wrong; this path shouldn't be hit", 500)
|
|
|
|
log.Printf("Internal routing error on %q", req.URL.Path)
|
|
|
|
}
|
|
|
|
|
2011-06-15 09:31:24 +00:00
|
|
|
func BadRequestError(conn http.ResponseWriter, errorMessage string, args ...interface{}) {
|
2010-07-26 03:34:04 +00:00
|
|
|
conn.WriteHeader(http.StatusBadRequest)
|
2011-06-15 09:31:24 +00:00
|
|
|
log.Printf("Bad request: %s", fmt.Sprintf(errorMessage, args...))
|
2010-07-26 03:34:04 +00:00
|
|
|
fmt.Fprintf(conn, "%s\n", errorMessage)
|
|
|
|
}
|
|
|
|
|
2011-06-15 09:31:24 +00:00
|
|
|
func ForbiddenError(conn http.ResponseWriter, errorMessage string, args ...interface{}) {
|
|
|
|
conn.WriteHeader(http.StatusForbidden)
|
|
|
|
log.Printf("Forbidden: %s", fmt.Sprintf(errorMessage, args...))
|
|
|
|
fmt.Fprintf(conn, "<h1>Forbidden</h1>")
|
|
|
|
}
|
|
|
|
|
2011-06-23 15:36:42 +00:00
|
|
|
func RequestEntityTooLargeError(conn http.ResponseWriter) {
|
|
|
|
conn.WriteHeader(http.StatusRequestEntityTooLarge)
|
2011-06-15 09:31:24 +00:00
|
|
|
fmt.Fprintf(conn, "<h1>Request entity is too large</h1>")
|
|
|
|
}
|
|
|
|
|
2012-09-16 22:20:49 +00:00
|
|
|
func ServerError(conn http.ResponseWriter, req *http.Request, err error) {
|
2010-07-26 03:34:04 +00:00
|
|
|
conn.WriteHeader(http.StatusInternalServerError)
|
2012-09-16 22:20:49 +00:00
|
|
|
if auth.LocalhostAuthorized(req) {
|
|
|
|
fmt.Fprintf(conn, "Server error: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(conn, "An internal error occured, sorry.")
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 22:42:56 +00:00
|
|
|
func ReturnJSON(conn http.ResponseWriter, data interface{}) {
|
2011-03-12 20:32:40 +00:00
|
|
|
conn.Header().Set("Content-Type", "text/javascript")
|
2011-06-09 19:54:53 +00:00
|
|
|
|
|
|
|
if m, ok := data.(map[string]interface{}); ok {
|
2011-06-09 22:00:20 +00:00
|
|
|
statusCode := 0
|
|
|
|
if t, ok := m["error"].(string); ok && len(t) > 0 {
|
|
|
|
statusCode = http.StatusInternalServerError
|
|
|
|
}
|
2011-06-09 19:54:53 +00:00
|
|
|
if t, ok := m["errorType"].(string); ok {
|
|
|
|
switch t {
|
|
|
|
case "server":
|
2011-06-09 22:00:20 +00:00
|
|
|
statusCode = http.StatusInternalServerError
|
2011-06-09 19:54:53 +00:00
|
|
|
case "input":
|
2011-06-09 22:00:20 +00:00
|
|
|
statusCode = http.StatusBadRequest
|
2011-06-09 19:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-09 22:00:20 +00:00
|
|
|
if statusCode != 0 {
|
|
|
|
conn.WriteHeader(statusCode)
|
|
|
|
}
|
2011-06-09 19:54:53 +00:00
|
|
|
}
|
|
|
|
|
2010-07-26 03:34:04 +00:00
|
|
|
bytes, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
2010-11-15 03:52:52 +00:00
|
|
|
BadRequestError(conn, fmt.Sprintf(
|
2010-07-26 03:34:04 +00:00
|
|
|
"JSON serialization error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn.Write(bytes)
|
|
|
|
conn.Write([]byte("\n"))
|
|
|
|
}
|
2011-05-06 00:15:32 +00:00
|
|
|
|
|
|
|
type PrefixHandler struct {
|
|
|
|
Prefix string
|
|
|
|
Handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrefixHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if !strings.HasPrefix(req.URL.Path, p.Prefix) {
|
|
|
|
http.Error(rw, "Inconfigured PrefixHandler", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("X-PrefixHandler-PathBase", p.Prefix)
|
|
|
|
req.Header.Set("X-PrefixHandler-PathSuffix", req.URL.Path[len(p.Prefix):])
|
|
|
|
p.Handler.ServeHTTP(rw, req)
|
|
|
|
}
|
2012-09-16 22:20:49 +00:00
|
|
|
|
|
|
|
// BaseURL returns the base URL (scheme + host and optional port +
|
|
|
|
// blobserver prefix) that should be used for requests (and responses)
|
|
|
|
// subsequent to req. The returned URL does not end in a trailing slash.
|
|
|
|
// The scheme and host:port are taken from urlStr if present,
|
|
|
|
// or derived from req otherwise.
|
|
|
|
// The prefix part comes from urlStr.
|
|
|
|
func BaseURL(urlStr string, req *http.Request) (string, error) {
|
|
|
|
var baseURL string
|
|
|
|
defaultURL, err := url.Parse(urlStr)
|
|
|
|
if err != nil {
|
|
|
|
return baseURL, err
|
|
|
|
}
|
|
|
|
prefix := path.Clean(defaultURL.Path)
|
|
|
|
scheme := "http"
|
|
|
|
if req.TLS != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
host := req.Host
|
|
|
|
if defaultURL.Host != "" {
|
|
|
|
host = defaultURL.Host
|
|
|
|
}
|
|
|
|
if defaultURL.Scheme != "" {
|
|
|
|
scheme = defaultURL.Scheme
|
|
|
|
}
|
|
|
|
baseURL = scheme + "://" + host + prefix
|
|
|
|
return baseURL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestTargetPort returns the port targetted by the client
|
|
|
|
// in req. If not present, it returns 80, or 443 if TLS is used.
|
|
|
|
func RequestTargetPort(req *http.Request) int {
|
|
|
|
_, portStr, err := net.SplitHostPort(req.Host)
|
|
|
|
if err == nil && portStr != "" {
|
|
|
|
port, err := strconv.ParseInt(portStr, 0, 64)
|
|
|
|
if err == nil {
|
|
|
|
return int(port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.TLS != nil {
|
|
|
|
return 443
|
|
|
|
}
|
|
|
|
return 80
|
|
|
|
}
|