FAQ
Hi,

Im hijacking http connection and closing it

func openPushChannel(comm_id string, join_time string) (chan string) {
var newUser commEntity
var userRecvChannel chan string

_, exists := users[comm_id]
if exists {
fmt.Printf("Already joined> User-id:%v %v\n", comm_id, join_time)
userRecvChannel = users[comm_id].recv
} else {
fmt.Printf("Join Chat> User-id:%v %v\n", comm_id, join_time)
newUser.id = comm_id
newUser.recv = make(chan string)
users[newUser.id] = newUser
userRecvChannel = newUser.recv
}
return userRecvChannel
}

func getChatMessage(recv chan string) (msg string) {
timeout := time.After(5 * time.Second)
select {
case newMessage := <-recv:
msg = newMessage
case <-timeout:
fmt.Printf("timeout yes\n")
msg = "timeout"
}
return msg
}

func joinChat(w http.ResponseWriter, req *http.Request) {

w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Content-Type", "text/html")

fmt.Printf("JoinChat method> User-id:%s\n", req.FormValue("comm_id"))
recv := openPushChannel(req.FormValue("comm_id"), req.FormValue("join_time"))
newMessage := getChatMessage(recv)

if newMessage == "timeout" {
conn, _, _ := w.(http.Hijacker).Hijack()
fmt.Printf("Timeout- close conn> User-id:%s %s %T\n", req.FormValue("comm_id"), req.FormValue("join_time"), conn)
conn.Close()
//response.Body.Close?
} else {
io.WriteString(w, newMessage)
fmt.Printf("Chat sent> User-id:%s %s %s\n", req.FormValue("comm_id"), req.FormValue("join_time"), newMessage)
}
}

func main() {
// make sure app uses all cores
flag.Parse()
runtime.GOMAXPROCS(*numCores)

fmt.Printf("\nChat server running at " +
"http://0.0.0.0:8000 on %d CPU cores\n", *numCores)

http.HandleFunc("/", testPage)
http.HandleFunc("/jquery.js", jquery)
http.HandleFunc("/chat/join/", joinChat)

err := http.ListenAndServe("0.0.0.0:8000", nil)
if err != nil {
log.Fatal("In main(): ", err)
}
}


However when its closed it is calling the same joinChat function again,
which is supposed to be called only via browser. very weird behavior
the code is available at
https://github.com/prudhviy/pp_chat/blob/master/chat.go

Reproducing Steps:
- run the chat.go program
- go to http://localhost:8000/
- enter 1 as User-id and click join chat button
- after 5 seconds the server closes the connection but in the command
prompt you can see joinChat method is called again somehow after
conn.Close()

Thanks,
bhaskar teja








--

Search Discussions

  • Bhaskar teja Yerneni at Oct 6, 2012 at 8:45 pm
    Im using go 1.0.3 on Linux mint
    Linux linux-mint 3.0.0-23-generic #39-Ubuntu SMP Thu Jul 19 19:19:11 UTC
    2012 x86_64 x86_64 x86_64 GNU/Linux

    On Sunday, October 7, 2012 2:08:48 AM UTC+5:30, Bhaskar teja Yerneni wrote:

    Hi,

    Im hijacking http connection and closing it

    func openPushChannel(comm_id string, join_time string) (chan string) {
    var newUser commEntity
    var userRecvChannel chan string

    _, exists := users[comm_id]
    if exists {
    fmt.Printf("Already joined> User-id:%v %v\n", comm_id, join_time)
    userRecvChannel = users[comm_id].recv
    } else {
    fmt.Printf("Join Chat> User-id:%v %v\n", comm_id, join_time)
    newUser.id = comm_id
    newUser.recv = make(chan string)
    users[newUser.id] = newUser
    userRecvChannel = newUser.recv
    }
    return userRecvChannel
    }

    func getChatMessage(recv chan string) (msg string) {
    timeout := time.After(5 * time.Second)
    select {
    case newMessage := <-recv:
    msg = newMessage
    case <-timeout:
    fmt.Printf("timeout yes\n")
    msg = "timeout"
    }
    return msg
    }

    func joinChat(w http.ResponseWriter, req *http.Request) {

    w.Header().Set("Cache-Control", "no-cache")
    w.Header().Set("Content-Type", "text/html")

    fmt.Printf("JoinChat method> User-id:%s\n", req.FormValue("comm_id"))
    recv := openPushChannel(req.FormValue("comm_id"), req.FormValue("join_time"))
    newMessage := getChatMessage(recv)

    if newMessage == "timeout" {
    conn, _, _ := w.(http.Hijacker).Hijack()
    fmt.Printf("Timeout- close conn> User-id:%s %s %T\n", req.FormValue("comm_id"), req.FormValue("join_time"), conn)
    conn.Close()
    //response.Body.Close?
    } else {
    io.WriteString(w, newMessage)
    fmt.Printf("Chat sent> User-id:%s %s %s\n", req.FormValue("comm_id"), req.FormValue("join_time"), newMessage)
    }
    }

    func main() {
    // make sure app uses all cores
    flag.Parse()
    runtime.GOMAXPROCS(*numCores)

    fmt.Printf("\nChat server running at " +
    "http://0.0.0.0:8000 on %d CPU cores\n", *numCores)

    http.HandleFunc("/", testPage)
    http.HandleFunc("/jquery.js", jquery)
    http.HandleFunc("/chat/join/", joinChat)

    err := http.ListenAndServe("0.0.0.0:8000", nil)
    if err != nil {
    log.Fatal("In main(): ", err)
    }
    }


    However when its closed it is calling the same joinChat function again,
    which is supposed to be called only via browser. very weird behavior
    the code is available at
    https://github.com/prudhviy/pp_chat/blob/master/chat.go

    Reproducing Steps:
    - run the chat.go program
    - go to http://localhost:8000/
    - enter 1 as User-id and click join chat button
    - after 5 seconds the server closes the connection but in the command
    prompt you can see joinChat method is called again somehow after
    conn.Close()

    Thanks,
    bhaskar teja







    --
  • Mathieu Lonjaret at Oct 6, 2012 at 10:08 pm
    There's nothing preventing your listener from accepting new
    connections is there? to make sure of that you should probably close
    the listener as well.

    On Sat, Oct 6, 2012 at 10:45 PM, Bhaskar teja Yerneni
    wrote:
    Im using go 1.0.3 on Linux mint
    Linux linux-mint 3.0.0-23-generic #39-Ubuntu SMP Thu Jul 19 19:19:11 UTC
    2012 x86_64 x86_64 x86_64 GNU/Linux


    On Sunday, October 7, 2012 2:08:48 AM UTC+5:30, Bhaskar teja Yerneni wrote:

    Hi,

    Im hijacking http connection and closing it

    func openPushChannel(comm_id string, join_time string) (chan string) {
    var newUser commEntity
    var userRecvChannel chan string

    _, exists := users[comm_id]
    if exists {
    fmt.Printf("Already joined> User-id:%v %v\n", comm_id, join_time)
    userRecvChannel = users[comm_id].recv
    } else {
    fmt.Printf("Join Chat> User-id:%v %v\n", comm_id, join_time)
    newUser.id = comm_id
    newUser.recv = make(chan string)
    users[newUser.id] = newUser
    userRecvChannel = newUser.recv
    }
    return userRecvChannel
    }

    func getChatMessage(recv chan string) (msg string) {
    timeout := time.After(5 * time.Second)
    select {
    case newMessage := <-recv:
    msg = newMessage
    case <-timeout:
    fmt.Printf("timeout yes\n")
    msg = "timeout"
    }
    return msg
    }

    func joinChat(w http.ResponseWriter, req *http.Request) {

    w.Header().Set("Cache-Control", "no-cache")
    w.Header().Set("Content-Type", "text/html")

    fmt.Printf("JoinChat method> User-id:%s\n", req.FormValue("comm_id"))
    recv := openPushChannel(req.FormValue("comm_id"),
    req.FormValue("join_time"))
    newMessage := getChatMessage(recv)

    if newMessage == "timeout" {
    conn, _, _ := w.(http.Hijacker).Hijack()
    fmt.Printf("Timeout- close conn> User-id:%s %s %T\n",
    req.FormValue("comm_id"), req.FormValue("join_time"), conn)
    conn.Close()
    //response.Body.Close?
    } else {
    io.WriteString(w, newMessage)
    fmt.Printf("Chat sent> User-id:%s %s %s\n",
    req.FormValue("comm_id"), req.FormValue("join_time"), newMessage)
    }
    }

    func main() {
    // make sure app uses all cores
    flag.Parse()
    runtime.GOMAXPROCS(*numCores)

    fmt.Printf("\nChat server running at " +
    "http://0.0.0.0:8000 on %d CPU cores\n", *numCores)

    http.HandleFunc("/", testPage)
    http.HandleFunc("/jquery.js", jquery)
    http.HandleFunc("/chat/join/", joinChat)

    err := http.ListenAndServe("0.0.0.0:8000", nil)
    if err != nil {
    log.Fatal("In main(): ", err)
    }
    }


    However when its closed it is calling the same joinChat function again,
    which is supposed to be called only via browser. very weird behavior
    the code is available at
    https://github.com/prudhviy/pp_chat/blob/master/chat.go

    Reproducing Steps:
    - run the chat.go program
    - go to http://localhost:8000/
    - enter 1 as User-id and click join chat button
    - after 5 seconds the server closes the connection but in the command
    prompt you can see joinChat method is called again somehow after
    conn.Close()

    Thanks,
    bhaskar teja






    --
    --
  • Bhaskar teja Yerneni at Oct 6, 2012 at 10:44 pm
    There is nothing preventing joinChat listener from accepting new connections. but I'm not making any new connection from JavaScript as I can see it in firebug net tab.

    joinChat listener should be able to accept new requests.
    As you can see I'm printing the timestamp which comes from JavaScript.
    and the timestamp is the same as the first request.
    I don't get what's the bug here,

    --
  • Bhaskar teja Yerneni at Oct 7, 2012 at 10:37 am
    The listener is not being called twice when i write something to the buffer
    rw of hijacked connection
    bufrw.WriteString("Timeout")
    bufrw.Flush()
    On Sunday, October 7, 2012 3:50:45 AM UTC+5:30, Bhaskar teja Yerneni wrote:

    There is nothing preventing joinChat listener from accepting new
    connections. but I'm not making any new connection from JavaScript as I can
    see it in firebug net tab.

    joinChat listener should be able to accept new requests.
    As you can see I'm printing the timestamp which comes from JavaScript.
    and the timestamp is the same as the first request.
    I don't get what's the bug here,
    --
  • DisposaBoy at Oct 7, 2012 at 1:20 pm

    On Sunday, October 7, 2012 11:37:23 AM UTC+1, Bhaskar teja Yerneni wrote:
    The listener is not being called twice when i write something to the
    buffer rw of hijacked connection
    bufrw.WriteString("Timeout")
    bufrw.Flush()
    FWIW, I can't reproduce this issue but I did notice that you're listening
    on `0.0.0.0` which means you're open to requests from outside your machine.
    So chances (as slim as it may be) are that your phantom request is coming
    from somewhere else

    On Sunday, October 7, 2012 3:50:45 AM UTC+5:30, Bhaskar teja Yerneni wrote:

    There is nothing preventing joinChat listener from accepting new
    connections. but I'm not making any new connection from JavaScript as I can
    see it in firebug net tab.

    joinChat listener should be able to accept new requests.
    As you can see I'm printing the timestamp which comes from JavaScript.
    and the timestamp is the same as the first request.
    I don't get what's the bug here,
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 6, '12 at 8:38p
activeOct 7, '12 at 1:20p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase