tinygo-org / drivers

TinyGo drivers for sensors, displays, wireless adaptors, and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
604 stars 188 forks source link

net/http: Fix http.Get() with port specification #435

Closed sago35 closed 2 years ago

sago35 commented 2 years ago

This PR corrects the behavior of http.Get(). Only http is affected, not https.

Two items were corrected.

  1. Change to pass only hostname to net.ParseIP
  2. Fix Port setting was always 80
sago35 commented 2 years ago

Before the correction, addresses such as http://hostname:8088/ were not accessible.

For example, the following is a std-Go program that listens for 8088, but could not connect to

package main

import (
    "fmt"
    "log"
    "net/http"
)

var (
    ip   = ""
    port = 8088
    cnt  = 0
)

func handler(w http.ResponseWriter, req *http.Request) {
    cnt++
    fmt.Fprintf(w, "hello %d\n", cnt)
    fmt.Printf("%d %#v\n", cnt, req.UserAgent())
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Printf("listen: %s:%d\n", ip, port)
    log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", ip, port), nil))
}
deadprogram commented 2 years ago

Thanks for the fix @sago35 now merging.