Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
|
1905db1f6a | |
|
3306e3cbbc | |
|
cd656f489e | |
|
c0f0414ff8 | |
|
47cd147ca0 |
|
@ -0,0 +1,86 @@
|
|||
package httppb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func (pb *HttpRequest) DumpRemoteAddr() string {
|
||||
return pb.RemoteAddr
|
||||
}
|
||||
|
||||
func (pb *HttpRequest) DumpUserAgent() string {
|
||||
var all string
|
||||
for param, values := range pb.Query {
|
||||
for _, value := range values {
|
||||
all += fmt.Sprint(" Query:", param, value)
|
||||
}
|
||||
}
|
||||
// hostname := r.URL.Query().Get("hostname")
|
||||
return pb.GetUserAgent() + all
|
||||
}
|
||||
|
||||
// todo: convert this code
|
||||
func (pb *HttpRequest) DumpClient() {
|
||||
log.Infof("%s %s %s\n", pb.Host, pb.URL, pb.RemoteAddr)
|
||||
/*
|
||||
var host, url, proto, addr, agent string
|
||||
|
||||
host = r.Host
|
||||
url = r.URL.String()
|
||||
proto = r.Proto
|
||||
addr = r.RemoteAddr
|
||||
agent = r.UserAgent()
|
||||
|
||||
log.Warn(host, proto, addr, url, agent)
|
||||
|
||||
fmt.Fprintln(accessf, time.Now(), host, proto, addr, url, agent)
|
||||
// return
|
||||
|
||||
fmt.Fprintln(clientf)
|
||||
fmt.Fprintln(clientf, time.Now())
|
||||
// Basic request information
|
||||
fmt.Fprintln(clientf, "Method:", r.Method)
|
||||
fmt.Fprintln(clientf, "URL:", r.URL)
|
||||
fmt.Fprintln(clientf, "Protocol:", r.Proto)
|
||||
fmt.Fprintln(clientf, "Host:", r.Host)
|
||||
fmt.Fprintln(clientf, "Remote Address:", r.RemoteAddr)
|
||||
|
||||
// Headers
|
||||
fmt.Fprintln(clientf, "Headers:")
|
||||
for name, values := range r.Header {
|
||||
for _, value := range values {
|
||||
fmt.Fprintln(clientf, "Headers:", name, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Query parameters
|
||||
fmt.Fprintln(clientf, "Query Parameters:")
|
||||
for param, values := range r.URL.Query() {
|
||||
for _, value := range values {
|
||||
fmt.Fprintln(clientf, "Query:", param, value)
|
||||
}
|
||||
}
|
||||
|
||||
// User-Agent
|
||||
fmt.Fprintln(clientf, "User-Agent:", r.UserAgent())
|
||||
|
||||
// Content Length
|
||||
fmt.Fprintln(clientf, "Content Length:", r.ContentLength)
|
||||
|
||||
// Cookies
|
||||
fmt.Fprintln(clientf, "Cookies:")
|
||||
for _, cookie := range r.Cookies() {
|
||||
fmt.Fprintln(clientf, cookie.Name, cookie.Value)
|
||||
}
|
||||
|
||||
// Request Body (if applicable)
|
||||
if r.Body != nil {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err == nil {
|
||||
fmt.Fprintln(clientf, "Body:", string(body))
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
|
@ -24,7 +24,10 @@ message HttpRequest { // HttpRequest repre
|
|||
int64 clientDataLen = 12; // len(body)
|
||||
bytes serverData = 13; // the server response
|
||||
int64 serverDataLen = 14; // len(data)
|
||||
repeated string log = 15; // use this to store whatever you want while the whole POST happens
|
||||
repeated string logs = 15; // use this to store whatever you want while the whole POST happens
|
||||
string userAgent = 16; // client user-agent
|
||||
map<string, string> query = 17; // r.URL.Query()
|
||||
string remoteAddr = 18; // r.RemoteAddr
|
||||
}
|
||||
|
||||
message HttpRequests { // `autogenpb:marshal` `autogenpb:mutex`
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package httppb
|
||||
|
||||
import "fmt"
|
||||
|
||||
func (pb *HttpRequest) Log(a ...any) {
|
||||
pb.Logs = append(pb.Logs, fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
func (pb *HttpRequest) Logf(s string, a ...any) {
|
||||
pb.Logs = append(pb.Logs, fmt.Sprintf(s, a...))
|
||||
}
|
10
reqToPB.go
10
reqToPB.go
|
@ -53,6 +53,13 @@ func ReqToPB(r *http.Request) (*HttpRequest, error) {
|
|||
}
|
||||
}
|
||||
|
||||
query := make(map[string]string)
|
||||
for param, values := range r.URL.Query() {
|
||||
if len(values) > 0 {
|
||||
query[param] = strings.Join(values, "\n")
|
||||
}
|
||||
}
|
||||
|
||||
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
||||
r.Body.Close()
|
||||
// log.Info("TRYING TO MARSHAL bytes:", len(msg), err)
|
||||
|
@ -67,6 +74,9 @@ func ReqToPB(r *http.Request) (*HttpRequest, error) {
|
|||
ClientData: msg,
|
||||
ClientDataLen: int64(len(msg)),
|
||||
Hostname: r.Header.Get("hostname"),
|
||||
UserAgent: r.UserAgent(),
|
||||
Query: query,
|
||||
RemoteAddr: r.RemoteAddr,
|
||||
}
|
||||
pb.Route = cleanURL(r.URL.Path)
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0
|
||||
|
||||
package httppb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// starts and sits waiting for HTTP requests
|
||||
func StartHTTP(okHandler func(http.ResponseWriter, *http.Request), port int) error {
|
||||
http.HandleFunc("/", okHandler)
|
||||
|
||||
p := fmt.Sprintf(":%d", port)
|
||||
log.Println("Running on port", p)
|
||||
|
||||
err := http.ListenAndServe(p, nil)
|
||||
if err != nil {
|
||||
log.Println("Error starting server:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0
|
||||
|
||||
package httppb
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func WriteFile(w http.ResponseWriter, resfork embed.FS, filename string) error {
|
||||
// fmt.Fprintln(w, "GOT TEST?")
|
||||
fullname := "resources/" + filename
|
||||
pfile, err := resfork.ReadFile(fullname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var repohtml string
|
||||
repohtml = string(pfile)
|
||||
if filename == "goReference.svg" {
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
}
|
||||
fmt.Fprintln(w, repohtml)
|
||||
log.Println("writeFile() found internal file:", filename)
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue