错误的HTTP状态码”/”在Go中是指格式错误的HTTP状态码。

huangapple go评论105阅读模式
英文:

malformed HTTP status code "/" error in Go

问题

Server.go

package main

import (
    "fmt"
    "net/http"
    "encoding/json"
    "io/ioutil"
    "strconv"
    "net"
    "bufio"
)

type Message struct {
    Text string
}

func Unmarshal(data []byte, v interface{}) error


func main() {

    //http.HandleFunc("/", handler)
    server,_ := net.Listen("tcp", ":" + strconv.Itoa(8080))
    if server == nil {
        panic("couldn't start listening: ")
    }
    conns := clientConns(server)
    for {
        go handleConn(<-conns)
    }

}


func clientConns(listener net.Listener) chan net.Conn {
    ch := make(chan net.Conn)
    i := 0
    go func() {
        for {
            client, _ := listener.Accept()
            if client == nil {
                fmt.Printf("couldn't accept: ")
                continue
            }
            i++
            fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
            ch <- client
        }
    }()
    return ch
}


func handleConn(client net.Conn) {
    b := bufio.NewReader(client)
    fmt.Println("Buffer")
    for {
        line, err := b.ReadBytes('\n')
        if err != nil { // EOF, or worse
            break
        }
        client.Write(line)
    }
}

Client.go

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "strings"
    "flag"
)

func Unmarshal(data []byte, v interface{}) error

func Marshal(v interface{}) ([]byte, error)


type Message struct {
    Text string
}

func main(){
    var flagtext = flag.String("flagtext", "Hello!", "Flag")
    flag.Parse()
    var text string
    text = *flagtext
    m := Message{text}
    var m1 Message
    b, err := json.Marshal(m)
    if err == nil{
        resp, err := http.Post("http://127.0.0.1:8080","application/json", strings.NewReader(string(b)))
        if err != nil{
            log.Fatal("Error while post: %v",err)
        }
        fmt.Println(resp)

        err = json.Unmarshal(b, &m1)
    }
}

Error I get when I run client.go is this:

Error while post: %vmalformed HTTP status code "/";

Though, the server registers a channel for each post, it shows a malformed HTTP status code. Is it because I'm listening in the wrong channel? I'm confused why this error is occurring.

英文:

Server.go

package main

import (
    &quot;fmt&quot;
    &quot;net/http&quot;
    //&quot;strings&quot;
   &quot;encoding/json&quot;
   &quot;io/ioutil&quot;
   &quot;strconv&quot;
    &quot;net&quot;
    &quot;bufio&quot;
)

type Message struct {
    Text string
}

func Unmarshal(data []byte, v interface{}) error


func main() {

    //http.HandleFunc(&quot;/&quot;, handler)
    server,_ := net.Listen(&quot;tcp&quot;, &quot;:&quot; + strconv.Itoa(8080))
    if server == nil {
        panic(&quot;couldn&#39;t start listening: &quot;)
    }
    conns := clientConns(server)
    for {
        go handleConn(&lt;-conns)
    }

}


func clientConns(listener net.Listener) chan net.Conn {
    ch := make(chan net.Conn)
    i := 0
    go func() {
        for {
            client, _ := listener.Accept()
            if client == nil {
                fmt.Printf(&quot;couldn&#39;t accept: &quot;)
                continue
            }
            i++
            fmt.Printf(&quot;%d: %v &lt;-&gt; %v\n&quot;, i, client.LocalAddr(), client.RemoteAddr())
            ch &lt;- client
        }
    }()
    return ch
}


func handleConn(client net.Conn) {
    b := bufio.NewReader(client)
    fmt.Println(&quot;Buffer&quot;)
    for {
        line, err := b.ReadBytes(&#39;\n&#39;)
        if err != nil { // EOF, or worse
            break
        }
        client.Write(line)
    }
}

Client.go

package main

import (
    &quot;encoding/json&quot;
    &quot;fmt&quot;
    &quot;log&quot;
    &quot;net/http&quot;
    &quot;strings&quot;
    &quot;flag&quot;
    //&quot;io&quot;
   // &quot;net&quot;
  //  &quot;net/rpc&quot;
//    &quot;sync&quot;
)

func Unmarshal(data []byte, v interface{}) error

func Marshal(v interface{}) ([]byte, error)


type Message struct {
    Text string
}

func main(){
    var flagtext = flag.String(&quot;flagtext&quot;, &quot;Hello!&quot;, &quot;Flag&quot;)
    flag.Parse()
    var text string
    text = *flagtext
    m := Message{text}
    var m1 Message
    b, err := json.Marshal(m)
    if err == nil{
        resp, err := http.Post(&quot;http://127.0.0.1:8080&quot;,&quot;application/json&quot;, strings.NewReader(string(b)))
        if err != nil{
            log.Fatal(&quot;Error while post: %v&quot;,err)
        }
        fmt.Println(resp)

        err = json.Unmarshal(b, &amp;m1)
    }
}

Error I get when I run client.go is this:

Error while post: %vmalformed HTTP status code &quot;/&quot;

Though, the server registers a channel for each post, it shows a malformed HTTP status code. Is it because I'm listening in the wrong channel? I'm confused why this error is occurring.

答案1

得分: 4

这行代码在服务器代码中:

client.Write(line)

将请求行发送回客户端。由于客户端发送的是类似于GET / HTTP/1.1的内容,这意味着服务器响应的内容也是类似于GET / HTTP/1.1,而不是类似于HTTP/1.1 200 OK。你看到的错误信息是因为在状态码位置出现了/

英文:

This line in the server code:

client.Write(line)

sends the request line back to the client. Since the client is posting something like GET / HTTP/1.1, this means that the server is responding with something like GET / HTTP/1.1, instead of something like HTTP/1.1 200 OK. The error-message you're seeing is because / appears in the status-code position.

答案2

得分: 2

在server.go中,你似乎试图从TCP套接字级别开始编写自己的HTTP服务器。这是不必要的工作-采取简单的方法并使用内置的HTTP服务器API。

这样的服务器的一般概述如下:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

并且在这篇文章中有进一步的描述。更多的文档在net/http中。

英文:

In server.go it seems you are trying to write your own HTTP server from the TCP socket level up. This is unnecessary work - take the easy route and use the built-in HTTP server API.

The general outline of such a server is like this:

package main

import (
    &quot;fmt&quot;
    &quot;net/http&quot;
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, &quot;Hi there, I love %s!&quot;, r.URL.Path[1:])
}

func main() {
    http.HandleFunc(&quot;/&quot;, handler)
    http.ListenAndServe(&quot;:8080&quot;, nil)
}

and is described further in this article. More documentation is in net/http.

huangapple
  • 本文由 发表于 2013年5月31日 08:37:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/16848087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定