Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP(x/http/client): net/http/client #13

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a02e126
WIP(x/http/get): Implementing get request using native socket
spongehah Jul 29, 2024
dc55abc
feat(x/http/get): Using libuv to speed up http.Get()
spongehah Jul 30, 2024
685154f
WIP(x/http-get): Use channels to pass responses
spongehah Aug 2, 2024
c52bdd5
WIP(x/http/client/get): Use channels to pass response(Passed the test)
spongehah Aug 5, 2024
763dd72
WIP(x/http/client/get): Some code optimization and comment addition
spongehah Aug 8, 2024
b9f4944
WIP(c/http/client): Add request timeout logic
spongehah Aug 9, 2024
2c9394c
WIP(x/http/client/get): Introducing textproto for header & implementi…
spongehah Aug 12, 2024
5744fd6
WIP(x/http/client/get): Extract the readTransfer function and complet…
spongehah Aug 13, 2024
2e9e338
WIP(x/http/client): Code tweaks made; Post request runs successfully.…
spongehah Aug 15, 2024
ba2a9d0
WIP(x/http/client): Implement http.Post() and redirection logic
spongehah Aug 16, 2024
1bad20a
WIP(x/http/client): http.PostForm() & some function improvements
spongehah Aug 19, 2024
d644d43
refactor(x/http/client): Move the file directory
spongehah Aug 20, 2024
2944a9d
WIP(x/http/client): http proxy & 100-continue & KeepAlive & gzip & so…
spongehah Aug 20, 2024
d05548e
WIP(x/http/client): Optimize readWriteLoop and make some code adjustm…
spongehah Aug 21, 2024
c2eb82d
WIP(x/http/client): bodyEOFSignal packaging & optimized readLoop() & …
spongehah Aug 22, 2024
a68bc29
WIP(x/http/client): Use body to wrap readCloser & optimize req.write …
spongehah Aug 23, 2024
cebff18
WIP(x/http/client): Categorize and write req.headers & perform unwrap…
spongehah Aug 26, 2024
c4d7315
WIP(x/net/http/client): Extract outwards libuv.Loop and timeout logic
spongehah Aug 29, 2024
0d8cc27
WIP(x/net/http/client): Implement IdleConnPool
spongehah Sep 4, 2024
e55b261
WIP(x/net/http/client): Implement BodyChunk
spongehah Sep 11, 2024
a4e8631
WIP(x/net/http/client): Mutiple eventLoop
spongehah Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/goplus/llgoexamples

go 1.20

require github.com/goplus/llgo v0.9.3-0.20240726020431-98d075728f2b
require (
github.com/goplus/llgo v0.9.8-0.20240919105235-c6436ea6d196
golang.org/x/net v0.28.0
)

require golang.org/x/text v0.17.0 // indirect
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/goplus/llgo v0.9.0 h1:yaJzQperGUafEaHc9VlVQVskIngacoTNweEXY0GRi0Q=
github.com/goplus/llgo v0.9.0/go.mod h1:M3UwiYdPZFyx7m2J0+6Ti1dYVA3uOO1WvSBocuE8N7M=
github.com/goplus/llgo v0.9.1-0.20240709104849-d6a38a567fda h1:UIPwlgzCb8dV/7WFMyprhZuq8CSLAQIqwFpH5AhrNOM=
github.com/goplus/llgo v0.9.1-0.20240709104849-d6a38a567fda/go.mod h1:zsrtWZapL4aklZc99xBSZRynGzLTIT1mLRjP0VSn9iw=
github.com/goplus/llgo v0.9.1-0.20240712060421-858d38d314a3 h1:2fZ2zQ8S58KvOsJTx6s6MHoi6n1K4sqQwIbTauMrgEE=
github.com/goplus/llgo v0.9.1-0.20240712060421-858d38d314a3/go.mod h1:zsrtWZapL4aklZc99xBSZRynGzLTIT1mLRjP0VSn9iw=
github.com/goplus/llgo v0.9.3-0.20240726020431-98d075728f2b h1:z9FUoeAALL5ytBhhGhE1dXm4+L1Q2eMUTcfiqLAZgf8=
github.com/goplus/llgo v0.9.3-0.20240726020431-98d075728f2b/go.mod h1:zsrtWZapL4aklZc99xBSZRynGzLTIT1mLRjP0VSn9iw=
github.com/goplus/llgo v0.9.8-0.20240919105235-c6436ea6d196 h1:LckJktvgChf3x0eex+GT//JkYRj1uiT4uMLzyrg3ChU=
github.com/goplus/llgo v0.9.8-0.20240919105235-c6436ea6d196/go.mod h1:5Fs+08NslqofJ7xtOiIXugkurYOoQvY02ZkFNWA1uEI=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
29 changes: 29 additions & 0 deletions x/net/http/_demo/chunked/chunked.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
resp, err := http.Get("http://localhost:8080/chunked")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
29 changes: 29 additions & 0 deletions x/net/http/_demo/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
resp, err := http.Get("https://www.baidu.com")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
52 changes: 52 additions & 0 deletions x/net/http/_demo/headers/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://www.baidu.com", nil)
if err != nil {
println(err.Error())
return
}

//req.Header.Set("accept", "*/*")
req.Header.Set("accept-encoding", "gzip")
//req.Header.Set("cache-control", "no-cache")
//req.Header.Set("pragma", "no-cache")
//req.Header.Set("priority", "u=0, i")
//req.Header.Set("referer", "https://jsonplaceholder.typicode.com/")
//req.Header.Set("sec-ch-ua", "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"")
//req.Header.Set("sec-ch-ua-mobile", "?0")
//req.Header.Set("sec-ch-ua-platform", "\"macOS\"")
//req.Header.Set("sec-fetch-dest", "document")
//req.Header.Set("sec-fetch-mode", "navigate")
//req.Header.Set("sec-fetch-site", "same-origin")
//req.Header.Set("sec-fetch-user", "?1")
////req.Header.Set("upgrade-insecure-requests", "1")
//req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")

resp, err := client.Do(req)
if err != nil {
println(err.Error())
return
}
defer resp.Body.Close()
fmt.Println(resp.Status)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
println(err.Error())
return
}
fmt.Println(string(body))
}
36 changes: 36 additions & 0 deletions x/net/http/_demo/maxConnsPerHost/maxConnsPerHost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
client := &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 2,
},
}
req, err := http.NewRequest("GET", "https://www.baidu.com", nil)
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
fmt.Println(resp.Proto)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
43 changes: 43 additions & 0 deletions x/net/http/_demo/parallelRequest/parallelRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"sync"

"github.com/goplus/llgoexamples/x/net/http"
)

func worker(id int, wg *sync.WaitGroup) {
defer wg.Done()
resp, err := http.Get("http://www.baidu.com")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(id, ":", resp.Status)
//body, err := io.ReadAll(resp.Body)
//if err != nil {
// fmt.Println(err)
// return
//}
//fmt.Println(string(body))
resp.Body.Close()
}

func main() {
var wait sync.WaitGroup
for i := 0; i < 500; i++ {
wait.Add(1)
go worker(i, &wait)
}
wait.Wait()
fmt.Println("All done")

resp, err := http.Get("http://www.baidu.com")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resp.Status)
resp.Body.Close()
}
31 changes: 31 additions & 0 deletions x/net/http/_demo/post/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"bytes"
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
data := []byte(`{"id":1,"title":"foo","body":"bar","userId":"1"}`)
resp, err := http.Post("https://jsonplaceholder.typicode.com/posts", "application/json; charset=UTF-8", bytes.NewBuffer(data))
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println(resp.Status)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
29 changes: 29 additions & 0 deletions x/net/http/_demo/postform/postform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"io"
"net/url"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
formData := url.Values{
"name": {"John Doe"},
"email": {"[email protected]"},
}

resp, err := http.PostForm("http://httpbin.org/post", formData)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
30 changes: 30 additions & 0 deletions x/net/http/_demo/redirect/redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
resp, err := http.Get("http://localhost:8080") // Start "../server/redirectServer.go" before running
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
fmt.Println(resp.Proto)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
50 changes: 50 additions & 0 deletions x/net/http/_demo/reuseConn/reuseConn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/net/http"
)

func main() {
// Send request first time
resp, err := http.Get("https://www.baidu.com")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
resp.Body.Close()

// Send request second time
resp, err = http.Get("https://www.baidu.com")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
body, err = io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
resp.Body.Close()
}
Loading
Loading