Skip to content

Instantly share code, notes, and snippets.

@mertyildiran
Created May 31, 2022 20:45
Show Gist options
  • Save mertyildiran/b2ca62abfcdfe1bd5da719760ed0e5c1 to your computer and use it in GitHub Desktop.
Save mertyildiran/b2ca62abfcdfe1bd5da719760ed0e5c1 to your computer and use it in GitHub Desktop.
Go HTTP request weird behavior
package main
import (
"fmt"
"net/http"
"strings"
)
// build with: go build -o main main.go
// Run gdb -x problem.gdb (breaks 3 times)
// Enable commented out lines
// Run gdb -x problem.gdb (breaks only once, why?)
// breakpoint: https://github.com/golang/go/blob/go1.17.6/src/net/sock_posix.go#L19
const ACCESS_TOKEN = "c798b14b83d519ffc531ef816cfce81e7d53367dbd5c4d3171a06ad888fabdaf"
func main() {
client := &http.Client{}
payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\[email protected]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nJohn\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"gender\"\r\n\r\nmale\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\nactive\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")
req, err := http.NewRequest("POST", "https://gorest.co.in/public/v2/users", payload)
if err != nil {
panic(err)
}
req.Header.Set("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
req.Header.Set("authorization", fmt.Sprintf("Bearer %s", ACCESS_TOKEN))
req.Header.Set("cache-control", "no-cache")
res, err := client.Do(req)
if err != nil {
panic(err)
}
fmt.Printf("res: %+v\n", res)
// body, err := io.ReadAll(res.Body)
// if err != nil {
// panic(err)
// }
// fmt.Printf("body: %v\n", string(body))
req, err = http.NewRequest("GET", "https://gorest.co.in/public/v2/users", nil)
if err != nil {
panic(err)
}
req.Header.Set("cache-control", "no-cache")
res, err = client.Do(req)
if err != nil {
panic(err)
}
fmt.Printf("res: %+v\n", res)
// body, err = io.ReadAll(res.Body)
// if err != nil {
// panic(err)
// }
// fmt.Printf("body: %v\n", string(body))
req, err = http.NewRequest("GET", "https://gorest.co.in/public/v2/posts", nil)
if err != nil {
panic(err)
}
req.Header.Set("cache-control", "no-cache")
res, err = client.Do(req)
if err != nil {
panic(err)
}
fmt.Printf("res: %+v\n", res)
// body, err = io.ReadAll(res.Body)
// if err != nil {
// panic(err)
// }
// fmt.Printf("body: %v\n", string(body))
}
file main
break net.socket
run
continue
continue
continue
Before:
#0 net.socket (ctx=..., net=..., family=2, sotype=1, proto=0, ipv6only=false, laddr=..., raddr=..., ctrlFn={void (string, string, syscall.RawConn, error *)} 0xc00012d4f8) at /usr/local/go/src/net/sock_posix.go:19
#1 0x000000000053f598 in net.internetSocket (ctx=..., net=..., laddr=..., raddr=..., sotype=1, proto=0, mode=..., ctrlFn={void (string, string, syscall.RawConn, error *)} 0xc00012d5a8) at /usr/local/go/src/net/ipsock_posix.go:142
#2 0x0000000000548a65 in net.(*sysDialer).doDialTCP (sd=0xc000130100, ctx=..., laddr=0x0, raddr=0xc0000bb2c0) at /usr/local/go/src/net/tcpsock_posix.go:66
#3 0x0000000000548959 in net.(*sysDialer).dialTCP (sd=<optimized out>, ctx=..., laddr=<optimized out>, raddr=<optimized out>) at /usr/local/go/src/net/tcpsock_posix.go:62
#4 0x000000000053020b in net.(*sysDialer).dialSingle (sd=0xc000130100, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>,
ra=<error reading variable: access outside bounds of object referenced via synthetic pointer>, c=..., err=...) at /usr/local/go/src/net/dial.go:583
#5 0x000000000052fb52 in net.(*sysDialer).dialSerial (sd=0xc000130100, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>, ras=..., ~r2=..., ~r3=...) at /usr/local/go/src/net/dial.go:551
#6 0x000000000052e936 in net.(*Dialer).DialContext (d=0xc0000aa240, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>,
network=<error reading variable: access outside bounds of object referenced via synthetic pointer>, address=<error reading variable: access outside bounds of object referenced via synthetic pointer>, ~r3=..., ~r4=...)
at /usr/local/go/src/net/dial.go:428
#7 0x00000000005f5a7b in net.(*Dialer).DialContext-fm (ctx=..., network=..., address=...) at /usr/local/go/src/net/dial.go:372
#8 0x00000000005e823a in net/http.(*Transport).dial (t=<optimized out>, ctx=..., network=..., addr=...) at /usr/local/go/src/net/http/transport.go:1166
#9 0x00000000005eb365 in net/http.(*Transport).dialConn (t=0x834cc0, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>, cm=..., pconn=0xc0000e07e0, err=...) at /usr/local/go/src/net/http/transport.go:1835
#10 0x00000000005e9bf0 in net/http.(*Transport).dialConnFor (t=0x6b3568 <timerCtx,context.Context>, w=0xc0000f02c0) at /usr/local/go/src/net/http/transport.go:1446
#11 0x00000000005e9b0a in net/http.(*Transport).queueForDial·dwrap·112 () at /usr/local/go/src/net/http/transport.go:1415
#12 0x0000000000462ec1 in runtime.goexit () at /usr/local/go/src/runtime/asm_amd64.s:1581
#13 0x0000000000000000 in ?? ()
After:
#0 net.socket (ctx=..., net=..., family=2, sotype=1, proto=0, ipv6only=false, laddr=..., raddr=..., ctrlFn={void (string, string, syscall.RawConn, error *)} 0xc0001874f8) at /usr/local/go/src/net/sock_posix.go:19
#1 0x000000000053f758 in net.internetSocket (ctx=..., net=..., laddr=..., raddr=..., sotype=1, proto=0, mode=..., ctrlFn={void (string, string, syscall.RawConn, error *)} 0xc0001875a8) at /usr/local/go/src/net/ipsock_posix.go:142
#2 0x0000000000548c25 in net.(*sysDialer).doDialTCP (sd=0xc000188100, ctx=..., laddr=0x0, raddr=0xc0000ad2c0) at /usr/local/go/src/net/tcpsock_posix.go:66
#3 0x0000000000548b19 in net.(*sysDialer).dialTCP (sd=<optimized out>, ctx=..., laddr=<optimized out>, raddr=<optimized out>) at /usr/local/go/src/net/tcpsock_posix.go:62
#4 0x00000000005303cb in net.(*sysDialer).dialSingle (sd=0xc000188100, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>,
ra=<error reading variable: access outside bounds of object referenced via synthetic pointer>, c=..., err=...) at /usr/local/go/src/net/dial.go:583
#5 0x000000000052fd12 in net.(*sysDialer).dialSerial (sd=0xc000188100, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>, ras=..., ~r2=..., ~r3=...) at /usr/local/go/src/net/dial.go:551
#6 0x000000000052eaf6 in net.(*Dialer).DialContext (d=0xc000098240, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>,
network=<error reading variable: access outside bounds of object referenced via synthetic pointer>, address=<error reading variable: access outside bounds of object referenced via synthetic pointer>, ~r3=..., ~r4=...)
at /usr/local/go/src/net/dial.go:428
#7 0x00000000005f5c3b in net.(*Dialer).DialContext-fm (ctx=..., network=..., address=...) at /usr/local/go/src/net/dial.go:372
#8 0x00000000005e83fa in net/http.(*Transport).dial (t=<optimized out>, ctx=..., network=..., addr=...) at /usr/local/go/src/net/http/transport.go:1166
#9 0x00000000005eb525 in net/http.(*Transport).dialConn (t=0x835cc0, ctx=<error reading variable: access outside bounds of object referenced via synthetic pointer>, cm=..., pconn=0xc0000c25a0, err=...) at /usr/local/go/src/net/http/transport.go:1835
#10 0x00000000005e9db0 in net/http.(*Transport).dialConnFor (t=0x6b45c8 <timerCtx,context.Context>, w=0xc0000d02c0) at /usr/local/go/src/net/http/transport.go:1446
#11 0x00000000005e9cca in net/http.(*Transport).queueForDial·dwrap·112 () at /usr/local/go/src/net/http/transport.go:1415
#12 0x0000000000462ec1 in runtime.goexit () at /usr/local/go/src/runtime/asm_amd64.s:1581
#13 0x0000000000000000 in ?? ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment