Skip to content

Instantly share code, notes, and snippets.

@stevenwilkin
Forked from teknoraver/unixhttpc.go
Created July 8, 2020 11:51
Show Gist options
  • Save stevenwilkin/86d14b24cec2c2a2d0a187cce958dc19 to your computer and use it in GitHub Desktop.
Save stevenwilkin/86d14b24cec2c2a2d0a187cce958dc19 to your computer and use it in GitHub Desktop.

Revisions

  1. @teknoraver teknoraver revised this gist Dec 22, 2016. 1 changed file with 21 additions and 5 deletions.
    26 changes: 21 additions & 5 deletions unixhttpc.go
    Original file line number Diff line number Diff line change
    @@ -2,28 +2,44 @@ package main

    import (
    "context"
    "flag"
    "fmt"
    "io"
    "net"
    "net/http"
    "os"
    "strings"
    )

    func main() {
    if len(os.Args) != 3 {
    fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "/path.socket /uri")
    return
    post := flag.String("d", "", "data to POST")
    help := flag.Bool("h", false, "usage help")
    flag.Parse()

    if *help || len(flag.Args()) != 2 {
    fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "[-d data] /path.socket /uri")
    flag.PrintDefaults()
    os.Exit(0)
    }

    fmt.Println("Unix HTTP client")

    httpc := http.Client{
    Transport: &http.Transport{
    DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
    return net.Dial("unix", os.Args[1])
    return net.Dial("unix", flag.Args()[0])
    },
    },
    }
    response, err := httpc.Get("http://unix" + os.Args[2])

    var response *http.Response
    var err error
    if len(*post) == 0 {
    response, err = httpc.Get("http://unix" + flag.Args()[1])
    } else {
    response, err = httpc.Post("http://unix"+flag.Args()[1], "application/octet-stream", strings.NewReader(*post))
    }

    if err != nil {
    panic(err)
    }
  2. @teknoraver teknoraver revised this gist Dec 19, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion unixhttpc.go
    Original file line number Diff line number Diff line change
    @@ -28,5 +28,4 @@ func main() {
    panic(err)
    }
    io.Copy(os.Stdout, response.Body)

    }
  3. @teknoraver teknoraver created this gist Dec 19, 2016.
    32 changes: 32 additions & 0 deletions unixhttpc.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    package main

    import (
    "context"
    "fmt"
    "io"
    "net"
    "net/http"
    "os"
    )

    func main() {
    if len(os.Args) != 3 {
    fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "/path.socket /uri")
    return
    }
    fmt.Println("Unix HTTP client")

    httpc := http.Client{
    Transport: &http.Transport{
    DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
    return net.Dial("unix", os.Args[1])
    },
    },
    }
    response, err := httpc.Get("http://unix" + os.Args[2])
    if err != nil {
    panic(err)
    }
    io.Copy(os.Stdout, response.Body)

    }
    34 changes: 34 additions & 0 deletions unixhttpd.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    package main

    import (
    "fmt"
    "net"
    "net/http"
    "os"
    )

    func main() {
    if len(os.Args) < 2 {
    fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "/path.sock [wwwroot]")
    return
    }

    fmt.Println("Unix HTTP server")

    root := "."
    if len(os.Args) > 2 {
    root = os.Args[2]
    }

    os.Remove(os.Args[1])

    server := http.Server{
    Handler: http.FileServer(http.Dir(root)),
    }

    unixListener, err := net.Listen("unix", os.Args[1])
    if err != nil {
    panic(err)
    }
    server.Serve(unixListener)
    }