登录
首页 >  Golang >  Go问答

golang的unix域套接字http服务器无效化

来源:stackoverflow

时间:2024-02-10 18:45:23 194浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《golang的unix域套接字http服务器无效化》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在尝试用 golang 编写一个简单的基于 unix 域套接字的 http 服务器。这是代码:

package main

import (
    "fmt"
    "log"
    "net/http"
    "net"
    "os"
    "encoding/json"
    "os/signal"
    "syscall"
)

type unixlistener struct {
    servermux *http.servemux
    socketpath string
    uid int
    gid int
    socketfilemode os.filemode
}

//////////////////////////////////////////////////////////////////////
// start the http server with unix socket.
//////////////////////////////////////////////////////////////////////
func (l *unixlistener) start() error {
    listener, err := net.listen("unix", l.socketpath)
    if err != nil {
        return err
    }
    if err := os.chown(l.socketpath, l.uid, l.gid); err != nil {
        return err
    }
    if err := os.chmod(l.socketpath, l.socketfilemode); err != nil {
        return err
    }
    go func(){
        shutdown(listener)
    }()

    svr := http.server{ 
        handler: l.servermux,
    }

    if err := svr.serve(listener); err != nil {
        return err
    } else {
        return nil
    }
}


//////////////////////////////////////////////////////////////////////
// shutdown the http server.
//////////////////////////////////////////////////////////////////////
func shutdown(listener net.listener) {
    c := make(chan os.signal, 1)
    signal.notify(c, syscall.sighup, syscall.sigint, syscall.sigterm, syscall.sigquit)
    s := <-c
    listener.close()
    log.fatalf("[fatal] caught a signal. sinal=%s", s)
}


func hello(w http.responsewriter, r *http.request) {
    log.println("received request %s", r.requesturi)
    fmt.fprintf(w, "hello world!\n")
    w.header().set("contenttype", "application/json")
    w.writeheader(http.statusok)
    data := map[string]string { "name" : "satish"}
    resp, _ := json.marshal(data)
    if _, err := w.write(resp); err != nil {
        fmt.printf("error writing response")
    }
}

func main() {
    mux := http.newservemux()
    mux.handlefunc("/hello", hello)
    unixlistener := unixlistener{
        servermux: mux,
        socketpath: "/temp/test.sock",
        uid: 501,
        gid: 20,
        socketfilemode: 0644,
    }
    if err := unixlistener.start(); err != nil {
        log.fatalf("[fatal] http server error: %s", err)
    }
}

但是当我将请求发送到如下所示的基于 uds 的服务器时,处理程序似乎没有被调用,给出 404 未找到错误。实现基于uds的http服务器的正确方法是什么以及如何向它发送http请求?

curl -vvv  --unix-socket /temp/test.sock  http:/hello
*   Trying /Users/sburnwal/Projects/ACI/temp/test.sock:0...
* Connected to hello (/temp/test.sock) port 80 (#0)
> GET / HTTP/1.1
> Host: hello
> User-Agent: curl/7.84.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Mon, 14 Nov 2022 02:38:11 GMT
< Content-Length: 19
< 
404 page not found
* Connection #0 to host hello left intact

正确答案


它似乎将命令行中的 http:/hello 解释为主机名并将其用作标头。然后请求 / 作为路径

尝试将命令行中的 http:/hello 替换为 http://hello/hello。我不确定单斜杠与双斜杠是否重要。只是看起来很可疑。

curl -vvv  --unix-socket /temp/test.sock  http://hello/hello

或者更改您的 go 代码以拥有 "/" 的响应处理程序,而不仅仅是 "/hello"

mux.HandleFunc("/hello", hello)
mux.HandleFunc("/", hello)

以上就是《golang的unix域套接字http服务器无效化》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>