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

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

malformed HTTP status code "/" error in Go

问题

Server.go

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "encoding/json"
  6. "io/ioutil"
  7. "strconv"
  8. "net"
  9. "bufio"
  10. )
  11. type Message struct {
  12. Text string
  13. }
  14. func Unmarshal(data []byte, v interface{}) error
  15. func main() {
  16. //http.HandleFunc("/", handler)
  17. server,_ := net.Listen("tcp", ":" + strconv.Itoa(8080))
  18. if server == nil {
  19. panic("couldn't start listening: ")
  20. }
  21. conns := clientConns(server)
  22. for {
  23. go handleConn(<-conns)
  24. }
  25. }
  26. func clientConns(listener net.Listener) chan net.Conn {
  27. ch := make(chan net.Conn)
  28. i := 0
  29. go func() {
  30. for {
  31. client, _ := listener.Accept()
  32. if client == nil {
  33. fmt.Printf("couldn't accept: ")
  34. continue
  35. }
  36. i++
  37. fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
  38. ch <- client
  39. }
  40. }()
  41. return ch
  42. }
  43. func handleConn(client net.Conn) {
  44. b := bufio.NewReader(client)
  45. fmt.Println("Buffer")
  46. for {
  47. line, err := b.ReadBytes('\n')
  48. if err != nil { // EOF, or worse
  49. break
  50. }
  51. client.Write(line)
  52. }
  53. }

Client.go

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "strings"
  8. "flag"
  9. )
  10. func Unmarshal(data []byte, v interface{}) error
  11. func Marshal(v interface{}) ([]byte, error)
  12. type Message struct {
  13. Text string
  14. }
  15. func main(){
  16. var flagtext = flag.String("flagtext", "Hello!", "Flag")
  17. flag.Parse()
  18. var text string
  19. text = *flagtext
  20. m := Message{text}
  21. var m1 Message
  22. b, err := json.Marshal(m)
  23. if err == nil{
  24. resp, err := http.Post("http://127.0.0.1:8080","application/json", strings.NewReader(string(b)))
  25. if err != nil{
  26. log.Fatal("Error while post: %v",err)
  27. }
  28. fmt.Println(resp)
  29. err = json.Unmarshal(b, &m1)
  30. }
  31. }

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

  1. 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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. //&quot;strings&quot;
  6. &quot;encoding/json&quot;
  7. &quot;io/ioutil&quot;
  8. &quot;strconv&quot;
  9. &quot;net&quot;
  10. &quot;bufio&quot;
  11. )
  12. type Message struct {
  13. Text string
  14. }
  15. func Unmarshal(data []byte, v interface{}) error
  16. func main() {
  17. //http.HandleFunc(&quot;/&quot;, handler)
  18. server,_ := net.Listen(&quot;tcp&quot;, &quot;:&quot; + strconv.Itoa(8080))
  19. if server == nil {
  20. panic(&quot;couldn&#39;t start listening: &quot;)
  21. }
  22. conns := clientConns(server)
  23. for {
  24. go handleConn(&lt;-conns)
  25. }
  26. }
  27. func clientConns(listener net.Listener) chan net.Conn {
  28. ch := make(chan net.Conn)
  29. i := 0
  30. go func() {
  31. for {
  32. client, _ := listener.Accept()
  33. if client == nil {
  34. fmt.Printf(&quot;couldn&#39;t accept: &quot;)
  35. continue
  36. }
  37. i++
  38. fmt.Printf(&quot;%d: %v &lt;-&gt; %v\n&quot;, i, client.LocalAddr(), client.RemoteAddr())
  39. ch &lt;- client
  40. }
  41. }()
  42. return ch
  43. }
  44. func handleConn(client net.Conn) {
  45. b := bufio.NewReader(client)
  46. fmt.Println(&quot;Buffer&quot;)
  47. for {
  48. line, err := b.ReadBytes(&#39;\n&#39;)
  49. if err != nil { // EOF, or worse
  50. break
  51. }
  52. client.Write(line)
  53. }
  54. }

Client.go

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. &quot;net/http&quot;
  7. &quot;strings&quot;
  8. &quot;flag&quot;
  9. //&quot;io&quot;
  10. // &quot;net&quot;
  11. // &quot;net/rpc&quot;
  12. // &quot;sync&quot;
  13. )
  14. func Unmarshal(data []byte, v interface{}) error
  15. func Marshal(v interface{}) ([]byte, error)
  16. type Message struct {
  17. Text string
  18. }
  19. func main(){
  20. var flagtext = flag.String(&quot;flagtext&quot;, &quot;Hello!&quot;, &quot;Flag&quot;)
  21. flag.Parse()
  22. var text string
  23. text = *flagtext
  24. m := Message{text}
  25. var m1 Message
  26. b, err := json.Marshal(m)
  27. if err == nil{
  28. resp, err := http.Post(&quot;http://127.0.0.1:8080&quot;,&quot;application/json&quot;, strings.NewReader(string(b)))
  29. if err != nil{
  30. log.Fatal(&quot;Error while post: %v&quot;,err)
  31. }
  32. fmt.Println(resp)
  33. err = json.Unmarshal(b, &amp;m1)
  34. }
  35. }

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

  1. 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

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

  1. client.Write(line)

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

英文:

This line in the server code:

  1. 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。

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

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func handler(w http.ResponseWriter, r *http.Request) {
  7. fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
  8. }
  9. func main() {
  10. http.HandleFunc("/", handler)
  11. http.ListenAndServe(":8080", nil)
  12. }

并且在这篇文章中有进一步的描述。更多的文档在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:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. )
  6. func handler(w http.ResponseWriter, r *http.Request) {
  7. fmt.Fprintf(w, &quot;Hi there, I love %s!&quot;, r.URL.Path[1:])
  8. }
  9. func main() {
  10. http.HandleFunc(&quot;/&quot;, handler)
  11. http.ListenAndServe(&quot;:8080&quot;, nil)
  12. }

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:

确定