refactored code to use wrapper functions when sending the header with content-type

This commit is contained in:
hyung-hwan 2025-01-13 20:17:49 +09:00
parent 091d28d52e
commit 0100d615d8
4 changed files with 123 additions and 94 deletions

View File

@ -199,7 +199,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
} }
c.cts_mtx.Unlock() c.cts_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(js); err != nil { goto oops } if err = je.Encode(js); err != nil { goto oops }
case http.MethodPost: case http.MethodPost:
@ -215,7 +215,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
err = json.NewDecoder(req.Body).Decode(&s) err = json.NewDecoder(req.Body).Decode(&s)
if err != nil || len(s.ServerAddrs) <= 0 { if err != nil || len(s.ServerAddrs) <= 0 {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
goto done goto done
} }
@ -223,10 +223,10 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
//cc.PeerAddrs = s.PeerAddrs //cc.PeerAddrs = s.PeerAddrs
cts, err = c.start_service(&cc) // TODO: this can be blocking. do we have to resolve addresses before calling this? also not good because resolution succeed or fail at each attempt. however ok as ServeHTTP itself is in a goroutine? cts, err = c.start_service(&cc) // TODO: this can be blocking. do we have to resolve addresses before calling this? also not good because resolution succeed or fail at each attempt. however ok as ServeHTTP itself is in a goroutine?
if err != nil { if err != nil {
status_code = http.StatusInternalServerError; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusInternalServerError)
if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops }
} else { } else {
status_code = http.StatusCreated; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusCreated)
if err = je.Encode(json_out_client_conn_id{Id: cts.id}); err != nil { goto oops } if err = je.Encode(json_out_client_conn_id{Id: cts.id}); err != nil { goto oops }
} }
@ -236,10 +236,10 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
// we do passive deletion rather than doing active deletion by calling // we do passive deletion rather than doing active deletion by calling
// c.RemoveAllClientConns() // c.RemoveAllClientConns()
c.ReqStopAllClientConns() c.ReqStopAllClientConns()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -268,14 +268,14 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
cts = c.FindClientConnById(ConnId(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
@ -310,16 +310,16 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
} }
cts.route_mtx.Unlock() cts.route_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(js); err != nil { goto oops } if err = je.Encode(js); err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
//c.RemoveClientConn(cts) //c.RemoveClientConn(cts)
cts.ReqStop() cts.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -347,14 +347,14 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id }); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id }); err != nil { goto oops }
goto done goto done
} }
cts = c.FindClientConnById(ConnId(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
@ -380,7 +380,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
} }
cts.route_mtx.Unlock() cts.route_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(jsp); err != nil { goto oops } if err = je.Encode(jsp); err != nil { goto oops }
case http.MethodPost: case http.MethodPost:
@ -392,26 +392,26 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
err = json.NewDecoder(req.Body).Decode(&jcr) err = json.NewDecoder(req.Body).Decode(&jcr)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
goto oops goto oops
} }
if jcr.ClientPeerAddr == "" { if jcr.ClientPeerAddr == "" {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
err = fmt.Errorf("blank client-peer-addr") err = fmt.Errorf("blank client-peer-addr")
goto oops goto oops
} }
server_peer_option = string_to_route_option(jcr.ServerPeerOption) server_peer_option = string_to_route_option(jcr.ServerPeerOption)
if server_peer_option == RouteOption(ROUTE_OPTION_UNSPEC) { if server_peer_option == RouteOption(ROUTE_OPTION_UNSPEC) {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
err = fmt.Errorf("wrong server-peer-option value - %s", server_peer_option) err = fmt.Errorf("wrong server-peer-option value - %s", server_peer_option)
goto oops goto oops
} }
lifetime, err = parse_duration_string(jcr.Lifetime) lifetime, err = parse_duration_string(jcr.Lifetime)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error()) err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error())
goto oops goto oops
} }
@ -429,20 +429,20 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
//cts.AddClientRouteConfig(rc) // TODO: this is to remember... but how to delete it? //cts.AddClientRouteConfig(rc) // TODO: this is to remember... but how to delete it?
r, err = cts.AddNewClientRoute(rc) r, err = cts.AddNewClientRoute(rc)
if err != nil { if err != nil {
status_code = http.StatusInternalServerError; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusInternalServerError)
if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops }
} else { } else {
status_code = http.StatusCreated; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusCreated)
if err = je.Encode(json_out_client_route_id{Id: r.id, CtsId: r.cts.id}); err != nil { goto oops } if err = je.Encode(json_out_client_route_id{Id: r.id, CtsId: r.cts.id}); err != nil { goto oops }
} }
case http.MethodDelete: case http.MethodDelete:
//cts.RemoveAllClientRoutes() //cts.RemoveAllClientRoutes()
cts.ReqStopAllClientRoutes() cts.ReqStopAllClientRoutes()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -474,34 +474,34 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
route_nid, err = strconv.ParseUint(route_id, 10, int(unsafe.Sizeof(RouteId(0)) * 8)) route_nid, err = strconv.ParseUint(route_id, 10, int(unsafe.Sizeof(RouteId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops }
goto done goto done
} }
cts = c.FindClientConnById(ConnId(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
r = cts.FindClientRouteById(RouteId(route_nid)) r = cts.FindClientRouteById(RouteId(route_nid))
if r == nil { if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent route id - " + route_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent route id - " + route_id}); err != nil { goto oops }
goto done goto done
} }
switch req.Method { switch req.Method {
case http.MethodGet: case http.MethodGet:
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
err = je.Encode(json_out_client_route{ err = je.Encode(json_out_client_route{
Id: r.id, Id: r.id,
ClientPeerAddr: r.peer_addr, ClientPeerAddr: r.peer_addr,
@ -519,13 +519,13 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
err = json.NewDecoder(req.Body).Decode(&jcr) err = json.NewDecoder(req.Body).Decode(&jcr)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
goto oops goto oops
} }
lifetime, err = parse_duration_string(jcr.Lifetime) lifetime, err = parse_duration_string(jcr.Lifetime)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error()) err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error())
goto oops goto oops
} }
@ -536,17 +536,18 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
err = r.ResetLifetime(lifetime) err = r.ResetLifetime(lifetime)
} }
if err != nil { if err != nil {
status_code = http.StatusForbidden; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusForbidden)
goto oops goto oops
} }
status_code = http.StatusOK; w.WriteHeader(status_code)
status_code = write_empty_resp_header(w, http.StatusOK)
case http.MethodDelete: case http.MethodDelete:
r.ReqStop() r.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -578,34 +579,34 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit
conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
port_nid, err = strconv.ParseUint(port_id, 10, int(unsafe.Sizeof(PortId(0)) * 8)) port_nid, err = strconv.ParseUint(port_id, 10, int(unsafe.Sizeof(PortId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong route id - " + port_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong route id - " + port_id}); err != nil { goto oops }
goto done goto done
} }
cts = c.FindClientConnById(ConnId(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
r = cts.FindClientRouteByServerPeerSvcPortId(PortId(port_nid)) r = cts.FindClientRouteByServerPeerSvcPortId(PortId(port_nid))
if r == nil { if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent server peer port id - " + port_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent server peer port id - " + port_id}); err != nil { goto oops }
goto done goto done
} }
switch req.Method { switch req.Method {
case http.MethodGet: case http.MethodGet:
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
err = je.Encode(json_out_client_route{ err = je.Encode(json_out_client_route{
Id: r.id, Id: r.id,
ClientPeerAddr: r.peer_addr, ClientPeerAddr: r.peer_addr,
@ -623,17 +624,17 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit
err = json.NewDecoder(req.Body).Decode(&jcr) err = json.NewDecoder(req.Body).Decode(&jcr)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
goto oops goto oops
} }
lifetime, err = parse_duration_string(jcr.Lifetime) lifetime, err = parse_duration_string(jcr.Lifetime)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error()) err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error())
goto oops goto oops
} else if lifetime < 0 { } else if lifetime < 0 {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
err = fmt.Errorf("negative lifetime value %s", jcr.Lifetime) err = fmt.Errorf("negative lifetime value %s", jcr.Lifetime)
goto oops goto oops
} }
@ -647,10 +648,10 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit
case http.MethodDelete: case http.MethodDelete:
r.ReqStop() r.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -681,20 +682,20 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers) ServeHTTP(w http.Response
conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
route_nid, err = strconv.ParseUint(route_id, 10, int(unsafe.Sizeof(RouteId(0)) * 8)) route_nid, err = strconv.ParseUint(route_id, 10, int(unsafe.Sizeof(RouteId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops }
goto done goto done
} }
r = c.FindClientRouteById(ConnId(conn_nid), RouteId(route_nid)) r = c.FindClientRouteById(ConnId(conn_nid), RouteId(route_nid))
if r == nil { if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent connection/route id - " + conn_id + "/" + route_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent connection/route id - " + conn_id + "/" + route_id}); err != nil { goto oops }
goto done goto done
} }
@ -717,15 +718,15 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers) ServeHTTP(w http.Response
} }
r.ptc_mtx.Unlock() r.ptc_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(jcp); err != nil { goto oops } if err = je.Encode(jcp); err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
r.ReqStopAllClientPeerConns() r.ReqStopAllClientPeerConns()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -759,26 +760,26 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo
conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
route_nid, err = strconv.ParseUint(route_id, 10, int(unsafe.Sizeof(RouteId(0)) * 8)) route_nid, err = strconv.ParseUint(route_id, 10, int(unsafe.Sizeof(RouteId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops }
goto done goto done
} }
peer_nid, err = strconv.ParseUint(peer_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) peer_nid, err = strconv.ParseUint(peer_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8))
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusBadRequest)
if err = je.Encode(json_errmsg{Text: "wrong peer id - " + peer_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "wrong peer id - " + peer_id}); err != nil { goto oops }
goto done goto done
} }
p = c.FindClientPeerConnById(ConnId(conn_nid), RouteId(route_nid), PeerId(peer_nid)) p = c.FindClientPeerConnById(ConnId(conn_nid), RouteId(route_nid), PeerId(peer_nid))
if p == nil { if p == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: "non-existent connection/route/peer id - " + conn_id + "/" + route_id + "/" + peer_id}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: "non-existent connection/route/peer id - " + conn_id + "/" + route_id + "/" + peer_id}); err != nil { goto oops }
goto done goto done
} }
@ -795,15 +796,15 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo
ServerLocalAddr: p.pts_laddr, ServerLocalAddr: p.pts_laddr,
} }
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(jcp); err != nil { goto oops } if err = je.Encode(jcp); err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
p.ReqStop() p.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -838,11 +839,11 @@ func (ctl *client_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request)
stats.ClientConns = c.stats.conns.Load() stats.ClientConns = c.stats.conns.Load()
stats.ClientRoutes = c.stats.routes.Load() stats.ClientRoutes = c.stats.routes.Load()
stats.ClientPeers = c.stats.peers.Load() stats.ClientPeers = c.stats.peers.Load()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(stats); err != nil { goto oops } if err = je.Encode(stats); err != nil { goto oops }
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
//done: //done:

29
hodu.go
View File

@ -157,3 +157,32 @@ func parse_duration_string(dur string) (time.Duration, error) {
if is_digit_or_period(get_last_rune_of_non_empty_string(tmp)) { tmp = tmp + "s" } if is_digit_or_period(get_last_rune_of_non_empty_string(tmp)) { tmp = tmp + "s" }
return time.ParseDuration(tmp) return time.ParseDuration(tmp)
} }
func write_json_resp_header(w http.ResponseWriter, status_code int) int {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status_code)
return status_code
}
func write_js_resp_header(w http.ResponseWriter, status_code int) int {
w.Header().Set("Content-Type", "application/javascript")
w.WriteHeader(status_code)
return status_code
}
func write_css_resp_header(w http.ResponseWriter, status_code int) int {
w.Header().Set("Content-Type", "text/css")
w.WriteHeader(status_code)
return status_code
}
func write_html_resp_header(w http.ResponseWriter, status_code int) int {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(status_code)
return status_code
}
func write_empty_resp_header(w http.ResponseWriter, status_code int) int {
w.WriteHeader(status_code)
return status_code
}

View File

@ -115,15 +115,15 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R
} }
s.cts_mtx.Unlock() s.cts_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(js); err != nil { goto oops } if err = je.Encode(js); err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
s.ReqStopAllServerConns() s.ReqStopAllServerConns()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
//done: //done:
@ -149,7 +149,7 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
conn_id = req.PathValue("conn_id") conn_id = req.PathValue("conn_id")
cts, err = s.FindServerConnByIdStr(conn_id) cts, err = s.FindServerConnByIdStr(conn_id)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops }
goto done goto done
} }
@ -180,16 +180,16 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
} }
cts.route_mtx.Unlock() cts.route_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(js); err != nil { goto oops } if err = je.Encode(js); err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
//s.RemoveServerConn(cts) //s.RemoveServerConn(cts)
cts.ReqStop() cts.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -215,7 +215,7 @@ func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
conn_id = req.PathValue("conn_id") conn_id = req.PathValue("conn_id")
cts, err = s.FindServerConnByIdStr(conn_id) cts, err = s.FindServerConnByIdStr(conn_id)
if err != nil { if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops }
goto done goto done
} }
@ -239,16 +239,16 @@ func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
} }
cts.route_mtx.Unlock() cts.route_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(jsp); err != nil { goto oops } if err = je.Encode(jsp); err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
//cts.RemoveAllServerRoutes() //cts.RemoveAllServerRoutes()
cts.ReqStopAllServerRoutes() cts.ReqStopAllServerRoutes()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -274,7 +274,7 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
if ctl.id == "wpx" && req.Method != http.MethodGet { if ctl.id == "wpx" && req.Method != http.MethodGet {
// support the get method only, if invoked via the wpx endpoint // support the get method only, if invoked via the wpx endpoint
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
goto done goto done
} }
@ -298,14 +298,14 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
} }
if err != nil { if err != nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusNotFound)
if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops } if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops }
goto done goto done
} }
switch req.Method { switch req.Method {
case http.MethodGet: case http.MethodGet:
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
err = je.Encode(json_out_server_route{ err = je.Encode(json_out_server_route{
Id: r.Id, Id: r.Id,
ClientPeerAddr: r.PtcAddr, ClientPeerAddr: r.PtcAddr,
@ -318,10 +318,10 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
case http.MethodDelete: case http.MethodDelete:
r.ReqStop() r.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNoContent)
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
done: done:
@ -363,11 +363,11 @@ func (ctl *server_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request)
stats.Extra = make(map[string]int64, len(s.stats.extra)) stats.Extra = make(map[string]int64, len(s.stats.extra))
for k, v = range s.stats.extra { stats.Extra[k] = v } for k, v = range s.stats.extra { stats.Extra[k] = v }
s.stats.extra_mtx.Unlock() s.stats.extra_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code) status_code = write_json_resp_header(w, http.StatusOK)
if err = je.Encode(stats); err != nil { goto oops } if err = je.Encode(stats); err != nil { goto oops }
default: default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadRequest)
} }
//done: //done:

View File

@ -358,13 +358,13 @@ func (pxy *server_proxy_http_main) ServeHTTP(w http.ResponseWriter, req *http.Re
r, err = pxy.get_route(req, in_wpx_mode) r, err = pxy.get_route(req, in_wpx_mode)
if err != nil { if err != nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNotFound)
goto oops goto oops
} }
/* /*
if r.SvcOption & (RouteOption(ROUTE_OPTION_HTTP) | RouteOption(ROUTE_OPTION_HTTPS)) == 0 { if r.SvcOption & (RouteOption(ROUTE_OPTION_HTTP) | RouteOption(ROUTE_OPTION_HTTPS)) == 0 {
status_code = http.StatusForbidden; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusForbidden)
err = fmt.Errorf("target not http/https") err = fmt.Errorf("target not http/https")
goto oops goto oops
} }
@ -372,7 +372,7 @@ func (pxy *server_proxy_http_main) ServeHTTP(w http.ResponseWriter, req *http.Re
addr = svc_addr_to_dst_addr(r.SvcAddr) addr = svc_addr_to_dst_addr(r.SvcAddr)
transport, err = pxy.addr_to_transport(s.ctx, addr) transport, err = pxy.addr_to_transport(s.ctx, addr)
if err != nil { if err != nil {
status_code = http.StatusBadGateway; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusBadGateway)
goto oops goto oops
} }
proxy_url = pxy.req_to_proxy_url(req, r) proxy_url = pxy.req_to_proxy_url(req, r)
@ -381,7 +381,7 @@ func (pxy *server_proxy_http_main) ServeHTTP(w http.ResponseWriter, req *http.Re
proxy_req, err = http.NewRequestWithContext(s.ctx, req.Method, proxy_url.String(), req.Body) proxy_req, err = http.NewRequestWithContext(s.ctx, req.Method, proxy_url.String(), req.Body)
if err != nil { if err != nil {
status_code = http.StatusInternalServerError; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusInternalServerError)
goto oops goto oops
} }
upgrade_required = mutate_proxy_req_headers(req, proxy_req, r.PathPrefix, in_wpx_mode) upgrade_required = mutate_proxy_req_headers(req, proxy_req, r.PathPrefix, in_wpx_mode)
@ -398,7 +398,7 @@ func (pxy *server_proxy_http_main) ServeHTTP(w http.ResponseWriter, req *http.Re
resp, err = client.Do(proxy_req) resp, err = client.Do(proxy_req)
//resp, err = transport.RoundTrip(proxy_req) // any advantage if using RoundTrip instead? //resp, err = transport.RoundTrip(proxy_req) // any advantage if using RoundTrip instead?
if err != nil { if err != nil {
status_code = http.StatusInternalServerError; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusInternalServerError)
goto oops goto oops
} else { } else {
status_code = resp.StatusCode status_code = resp.StatusCode
@ -448,7 +448,7 @@ func (pxy *server_proxy_http_wpx) ServeHTTP(w http.ResponseWriter, req *http.Req
var status_code int var status_code int
// var err error // var err error
status_code = http.StatusForbidden; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusForbidden)
// TODO: show the list of services running instead if enabled? // TODO: show the list of services running instead if enabled?
@ -474,16 +474,13 @@ func (pxy *server_proxy_xterm_file) ServeHTTP(w http.ResponseWriter, req *http.R
switch pxy.file { switch pxy.file {
case "xterm.js": case "xterm.js":
w.Header().Set("Content-Type", "text/javascript") status_code = write_js_resp_header(w, http.StatusOK)
status_code = http.StatusOK; w.WriteHeader(status_code)
w.Write(xterm_js) w.Write(xterm_js)
case "xterm-addon-fit.js": case "xterm-addon-fit.js":
w.Header().Set("Content-Type", "text/javascript") status_code = write_js_resp_header(w, http.StatusOK)
status_code = http.StatusOK; w.WriteHeader(status_code)
w.Write(xterm_addon_fit_js) w.Write(xterm_addon_fit_js)
case "xterm.css": case "xterm.css":
w.Header().Set("Content-Type", "text/css") status_code = write_css_resp_header(w, http.StatusOK)
status_code = http.StatusOK; w.WriteHeader(status_code)
w.Write(xterm_css) w.Write(xterm_css)
case "xterm.html": case "xterm.html":
var tmpl *template.Template var tmpl *template.Template
@ -505,7 +502,7 @@ func (pxy *server_proxy_xterm_file) ServeHTTP(w http.ResponseWriter, req *http.R
_, err = s.FindServerRouteByIdStr(conn_id, route_id) _, err = s.FindServerRouteByIdStr(conn_id, route_id)
} }
if err != nil { if err != nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNotFound)
goto oops goto oops
} }
@ -516,27 +513,29 @@ func (pxy *server_proxy_xterm_file) ServeHTTP(w http.ResponseWriter, req *http.R
_, err = tmpl.Parse(xterm_html) _, err = tmpl.Parse(xterm_html)
} }
if err != nil { if err != nil {
status_code = http.StatusInternalServerError; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusInternalServerError)
goto oops goto oops
} else { } else {
w.Header().Set("Content-Type", "text/html") status_code = write_html_resp_header(w, http.StatusOK)
w.WriteHeader(http.StatusOK)
tmpl.Execute(w, tmpl.Execute(w,
&server_proxy_xterm_session_info{ &server_proxy_xterm_session_info{
ConnId: conn_id, ConnId: conn_id,
RouteId: route_id, RouteId: route_id,
}) })
} }
case "_redirect": case "_redirect":
// shorthand for /_ssh/{conn_id}/_/ // shorthand for /_ssh/{conn_id}/_/
// don't care about parameters following the path // don't care about parameters following the path
status_code = http.StatusMovedPermanently status_code = http.StatusMovedPermanently
w.Header().Set("Location", req.URL.Path + "_/") w.Header().Set("Location", req.URL.Path + "_/")
w.WriteHeader(status_code) w.WriteHeader(status_code)
case "_forbidden": case "_forbidden":
status_code = http.StatusForbidden; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusForbidden)
default: default:
status_code = http.StatusNotFound; w.WriteHeader(status_code) status_code = write_empty_resp_header(w, http.StatusNotFound)
} }
//done: //done: