evio/examples/http-server/main.go

222 lines
5.3 KiB
Go
Raw Normal View History

2017-11-02 18:08:18 +03:00
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2017-10-28 03:01:03 +03:00
package main
import (
2017-11-04 16:26:00 +03:00
"bytes"
2017-11-01 03:44:57 +03:00
"flag"
2017-10-28 03:01:03 +03:00
"fmt"
"log"
2017-11-04 02:39:28 +03:00
"os"
2017-10-28 03:01:03 +03:00
"strconv"
"strings"
"time"
2017-10-29 00:58:59 +03:00
"github.com/tidwall/evio"
2017-10-28 03:01:03 +03:00
)
2017-11-02 23:29:13 +03:00
var res string
2017-10-28 03:01:03 +03:00
type request struct {
2017-10-28 22:23:13 +03:00
proto, method string
path, query string
head, body string
remoteAddr string
2017-10-28 03:01:03 +03:00
}
func main() {
2017-11-01 03:44:57 +03:00
var port int
var loops int
2017-11-02 23:29:13 +03:00
var aaaa bool
2017-11-03 23:22:31 +03:00
var noparse bool
2017-11-07 19:52:16 +03:00
var unixsocket string
var stdlib bool
2017-11-07 19:52:16 +03:00
flag.StringVar(&unixsocket, "unixsocket", "", "unix socket")
2017-11-01 03:44:57 +03:00
flag.IntVar(&port, "port", 8080, "server port")
2017-11-02 23:29:13 +03:00
flag.BoolVar(&aaaa, "aaaa", false, "aaaaa....")
2017-11-03 23:22:31 +03:00
flag.BoolVar(&noparse, "noparse", true, "do not parse requests")
flag.BoolVar(&stdlib, "stdlib", false, "use stdlib")
flag.IntVar(&loops, "loops", 0, "num loops")
2017-11-01 03:44:57 +03:00
flag.Parse()
2017-11-04 02:39:28 +03:00
if os.Getenv("NOPARSE") == "1" {
noparse = true
}
2017-11-02 23:29:13 +03:00
if aaaa {
res = strings.Repeat("a", 1024)
} else {
res = "Hello World!\r\n"
}
2017-10-29 00:58:59 +03:00
var events evio.Events
events.NumLoops = loops
events.Serving = func(srv evio.Server) (action evio.Action) {
log.Printf("http server started on port %d (loops: %d)", port, srv.NumLoops)
2017-11-07 19:52:16 +03:00
if unixsocket != "" {
log.Printf("http server started at %s", unixsocket)
}
if stdlib {
log.Printf("stdlib")
}
2017-10-28 03:01:03 +03:00
return
}
events.Opened = func(c evio.Conn) (out []byte, opts evio.Options, action evio.Action) {
c.SetContext(&evio.InputStream{})
//log.Printf("opened: laddr: %v: raddr: %v", c.LocalAddr(), c.RemoteAddr())
2017-10-28 03:01:03 +03:00
return
}
events.Closed = func(c evio.Conn, err error) (action evio.Action) {
//log.Printf("closed: %s: %s", c.LocalAddr().String(), c.RemoteAddr().String())
2017-10-28 03:01:03 +03:00
return
}
events.Data = func(c evio.Conn, in []byte) (out []byte, action evio.Action) {
2017-11-01 03:44:57 +03:00
if in == nil {
return
}
is := c.Context().(*evio.InputStream)
data := is.Begin(in)
2017-11-04 16:26:00 +03:00
if noparse && bytes.Contains(data, []byte("\r\n\r\n")) {
2017-11-04 02:39:28 +03:00
// for testing minimal single packet request -> response.
2017-11-06 14:48:10 +03:00
out = appendresp(nil, "200 OK", "", res)
2017-11-03 23:22:31 +03:00
return
}
2017-10-28 22:23:13 +03:00
// process the pipeline
2017-10-28 03:01:03 +03:00
var req request
for {
leftover, err := parsereq(data, &req)
if err != nil {
// bad thing happened
2017-10-28 22:23:13 +03:00
out = appendresp(out, "500 Error", "", err.Error()+"\n")
2017-10-29 00:58:59 +03:00
action = evio.Close
2017-10-28 03:01:03 +03:00
break
} else if len(leftover) == len(data) {
// request not ready, yet
break
}
// handle the request
req.remoteAddr = c.RemoteAddr().String()
2017-10-28 22:23:13 +03:00
out = appendhandle(out, &req)
2017-10-28 03:01:03 +03:00
data = leftover
}
is.End(data)
2017-10-28 03:01:03 +03:00
return
}
var ssuf string
if stdlib {
ssuf = "-net"
}
2017-11-02 03:36:35 +03:00
// We at least want the single http address.
addrs := []string{fmt.Sprintf("tcp"+ssuf+"://:%d", port)}
2017-11-07 19:52:16 +03:00
if unixsocket != "" {
addrs = append(addrs, fmt.Sprintf("unix"+ssuf+"://%s", unixsocket))
2017-11-07 19:52:16 +03:00
}
2017-11-02 03:36:35 +03:00
// Start serving!
log.Fatal(evio.Serve(events, addrs...))
2017-10-28 03:01:03 +03:00
}
2017-10-28 22:23:13 +03:00
// appendhandle handles the incoming request and appends the response to
// the provided bytes, which is then returned to the caller.
func appendhandle(b []byte, req *request) []byte {
2017-11-02 23:29:13 +03:00
return appendresp(b, "200 OK", "", res)
2017-10-28 03:01:03 +03:00
}
2017-10-28 22:23:13 +03:00
// appendresp will append a valid http response to the provide bytes.
// The status param should be the code plus text such as "200 OK".
// The head parameter should be a series of lines ending with "\r\n" or empty.
func appendresp(b []byte, status, head, body string) []byte {
2017-10-28 03:01:03 +03:00
b = append(b, "HTTP/1.1"...)
b = append(b, ' ')
b = append(b, status...)
b = append(b, '\r', '\n')
2017-11-02 23:29:13 +03:00
b = append(b, "Server: evio\r\n"...)
2017-10-28 03:01:03 +03:00
b = append(b, "Date: "...)
b = time.Now().AppendFormat(b, "Mon, 02 Jan 2006 15:04:05 GMT")
b = append(b, '\r', '\n')
if len(body) > 0 {
b = append(b, "Content-Length: "...)
b = strconv.AppendInt(b, int64(len(body)), 10)
b = append(b, '\r', '\n')
}
b = append(b, head...)
b = append(b, '\r', '\n')
if len(body) > 0 {
b = append(b, body...)
}
return b
}
2017-10-28 22:23:13 +03:00
// parsereq is a very simple http request parser. This operation
// waits for the entire payload to be buffered before returning a
// valid request.
2017-10-28 03:01:03 +03:00
func parsereq(data []byte, req *request) (leftover []byte, err error) {
2017-11-02 23:29:13 +03:00
sdata := string(data)
var i, s int
2017-10-28 03:01:03 +03:00
var top string
var clen int
2017-11-02 23:29:13 +03:00
var q = -1
// method, path, proto line
for ; i < len(sdata); i++ {
if sdata[i] == ' ' {
req.method = sdata[s:i]
for i, s = i+1, i+1; i < len(sdata); i++ {
if sdata[i] == '?' && q == -1 {
q = i - s
} else if sdata[i] == ' ' {
if q != -1 {
req.path = sdata[s:q]
req.query = req.path[q+1 : i]
} else {
req.path = sdata[s:i]
}
for i, s = i+1, i+1; i < len(sdata); i++ {
if sdata[i] == '\n' && sdata[i-1] == '\r' {
req.proto = sdata[s:i]
i, s = i+1, i+1
break
}
2017-10-28 22:23:13 +03:00
}
2017-11-02 23:29:13 +03:00
break
2017-10-28 22:23:13 +03:00
}
2017-11-02 23:29:13 +03:00
}
break
}
}
if req.proto == "" {
return data, fmt.Errorf("malformed request")
}
top = sdata[:s]
for ; i < len(sdata); i++ {
if i > 1 && sdata[i] == '\n' && sdata[i-1] == '\r' {
line := sdata[s : i-1]
s = i + 1
if line == "" {
req.head = sdata[len(top)+2 : i+1]
2017-10-28 03:01:03 +03:00
i++
if clen > 0 {
2017-11-02 23:29:13 +03:00
if len(sdata[i:]) < clen {
2017-10-28 03:01:03 +03:00
break
}
2017-11-02 23:29:13 +03:00
req.body = sdata[i : i+clen]
2017-10-28 03:01:03 +03:00
i += clen
}
return data[i:], nil
2017-11-02 23:29:13 +03:00
}
if strings.HasPrefix(line, "Content-Length:") {
n, err := strconv.ParseInt(strings.TrimSpace(line[len("Content-Length:"):]), 10, 64)
if err == nil {
clen = int(n)
2017-10-28 03:01:03 +03:00
}
}
}
}
// not enough data
return data, nil
}