allow 'OPTIONS' aka 'CORS' on http sockets

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-25 13:10:25 -07:00
parent 58b80b499a
commit 7110856b18
3 changed files with 14 additions and 25 deletions

View File

@ -3,7 +3,9 @@
all:
go run alfonso.go
run:
# this is the raw socket server. it will
# show all socket connects recieved
raw-socket:
go run server.go
build:

View File

@ -13,6 +13,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
func handler2(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
spew.Dump(r)
fmt.Fprintf(w, "Hi there, handler2 %s!", r.URL.Path[1:])
@ -30,9 +34,13 @@ func handler2(w http.ResponseWriter, r *http.Request) {
case http.MethodDelete:
// Remove the record.
log.Printf("handler2 DELETE")
case http.MethodOptions:
// Remove the record.
log.Printf("handler2 OPTIONS")
default:
// Give an error message.
log.Printf("handler2 DEFAULT")
log.Printf("handler2 DEFAULT Method=", r.Method)
spew.Dump(r.Method)
}
}

View File

@ -20,7 +20,7 @@ import "github.com/davecgh/go-spew/spew"
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_PORT = "9000"
CONN_TYPE = "tcp"
)
@ -38,20 +38,6 @@ func random() int {
}
func main() {
/*
// redirect all this output to systemd
dnssecsocket.UseJournalctl()
hostname := fqdn.Get()
log.Println("FQDN hostname is", hostname)
// lookup the IP address from DNS
dnsRR := dnssecsocket.Dnstrace(hostname, "AAAA")
spew.Dump(dnsRR)
ipaddr := dns.Field(dnsRR, 1)
log.Println("ipaddr", ipaddr)
*/
listenstr := "[" + "localhost" + "]:" + CONN_PORT
log.Println("listenstr", listenstr)
@ -103,6 +89,7 @@ func HandleConnection(conn *net.TCPConn) {
log.Println("Waiting for the client to tell me its name")
netData, err := newreader.ReadString('\n')
spew.Dump(netData)
log.Println("netData =", string(netData))
if err != nil {
log.Println(err)
return
@ -124,14 +111,6 @@ func HandleConnection(conn *net.TCPConn) {
}
log.Println("Recieved: ", temp)
if (temp == "list") {
log.Println("Should run list here")
}
if (temp == "cpuinfo") {
log.Println("Should cat /proc/cpuinfo")
}
result := strconv.Itoa(random()) + "\n"
conn.Write([]byte(string(result)))
return