0
votes

I a simple TCP server that accept connection , Get a URL using http.Get and then encode the response using gob. But gob fails to encode http.gzipReader. On encoding it gives following error message:

gob: type not registered for interface: http.gzipReader

My code is given below:

package main
import (
    "encoding/gob"
    "fmt"
    "log"
    "net"
    "net/http"
    "os"
)
const (
    CONN_HOST = "localhost"
    CONN_PORT = "3333"
    CONN_TYPE = "tcp"
)

func main() {
    // Listen for incoming connections.
    l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
    if err != nil {
        fmt.Println("Error listening:", err.Error())
        os.Exit(1)
    }
    for {
        conn, err := l.Accept()
        if err != nil {
            fmt.Println("Error accepting: ", err.Error())
            os.Exit(1)
        }
        go handleRequest(conn)
    }
}
func handleRequest(conn net.Conn) {
    res, err :=
        http.Get("http://info.cern.ch/hypertext/WWW/TheProject.html")
    if err != nil {
        log.Fatal(err)
    } else {
        encoder := gob.NewEncoder(conn)
        er := encoder.Encode(res)
        if er != nil {
            fmt.Println(er)
        }
    }
    conn.Write([]byte("Message received."))
    conn.Close()
}

Is there any way to encode http.gzipReaderc ?? I saw an example of gob.Register() , but I am trying to registrer http.gzipReaderc . But unable to do so. can any one suggest me solution to this problem ?

1
I got this error: gob: type not registered for interface: http.bodyEOFSignal. Are sure that you want to encode the whole part of http.Response? The content (HTML page) of http.Get response is available through Body field (which is an interface and the data itself may not yet available immediately or it may be encoded in gzip etc.) and you should read it first into a buffer (or create a wrapper) then passing it to the gob encoder. - putu

1 Answers

0
votes

I solved this issue by implementing my own ReadCloser. Check HTTP Caching Client