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

feat(x/net/http): Implement server and client function #17

Open
wants to merge 56 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 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
d25cbfe
feat(x/net/http): Init server skeleton
hackerchai Aug 14, 2024
9fb6f37
feat(x/net/http): Implement server conn logic and modify response & r…
hackerchai Aug 15, 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
19f6425
feat(x/net/http): Implement resonse and request logic
hackerchai Aug 16, 2024
42e5881
feat(x/net/http): Implement http server demo
hackerchai Aug 16, 2024
953f618
fix(x/net/http): Fix demo and multiple fixes
hackerchai Aug 19, 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
6f115f1
fix(x/net/http): Fix request receive logic
hackerchai 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
09cd8fe
refactor(x/net/http/demo): Update http demo
hackerchai Sep 3, 2024
15e84fb
refactor(x/net/http): Update new server logic
hackerchai Sep 3, 2024
6b6cfdd
refactor(x/net/http): Rewrite request implementation use host header
hackerchai Sep 3, 2024
0d8cc27
WIP(x/net/http/client): Implement IdleConnPool
spongehah Sep 4, 2024
16054ae
refactor(x/net/http): Rewrite request logic
hackerchai Sep 4, 2024
f03b73f
refactor(x/net/ht
hackerchai Sep 5, 2024
3f94d3f
refactor(x/net/http): Temporarily fix pipe write stuck
hackerchai Sep 5, 2024
757e3a1
refactor(x/net/http): Rewrite getBodyData logic using hyper_body_data
hackerchai Sep 6, 2024
650cb6c
feat(x/net/http): Implement responseStream reader & writer
hackerchai Sep 9, 2024
34ace8f
feat(x/net/http): Implement RequestBody logic
hackerchai Sep 9, 2024
799e91a
refactor(x/net/http/demo): Update demo
hackerchai Sep 10, 2024
94af890
refactor(x/net/http/demo): Remove response_stream
hackerchai Sep 10, 2024
75c69d5
refactor(x/net/http): Implement requestBody logic
hackerchai Sep 10, 2024
0250377
refactor(x/net/http/demo): Implement requestBody logic and intergrate…
hackerchai Sep 11, 2024
e55b261
WIP(x/net/http/client): Implement BodyChunk
spongehah Sep 11, 2024
67f1d21
refactor(x/net/http/demo): Neat http echo demo
hackerchai Sep 12, 2024
0ce31d6
refactor(x/net/http/demo): Re-implement requestBody
hackerchai Sep 12, 2024
3fae8cd
fix(x/net/http): Fix nil pointer error & optimize naming
hackerchai Sep 12, 2024
22ab2d5
fix(x/net/http): Remove updateConnRegistrations unsuse args
hackerchai Sep 12, 2024
e1c3717
refactor(x/net/http): Rewrite loop <-> hyper.executor logic
hackerchai Sep 12, 2024
8420957
fix(x/net/http): Remove unsused sync.Once
hackerchai Sep 12, 2024
03b3d7b
fix(x/net/http): Implement multi-thread eventLoop
hackerchai Sep 19, 2024
e8ea412
refactor(x/net/http): Implement close logic & other optimize
hackerchai Sep 19, 2024
a4e8631
WIP(x/net/http/client): Mutiple eventLoop
spongehah Sep 20, 2024
49afcb5
fix(x/net/http): Fix import name and adapt to future merge with client
hackerchai Sep 20, 2024
b26931c
Merge remote-tracking branch 'zhao/x/http-get' into feature/x-net-htt…
hackerchai Sep 20, 2024
0abdeb0
refactor(x/net/http/demo): Refactor file structure & rename
hackerchai Sep 20, 2024
6fdc040
refactor(x/net/http): Rename into bodyStream
hackerchai Sep 20, 2024
4bf50fe
refactor(x/net/http): Multiple fixes & adapt bodyStream struct
hackerchai Sep 20, 2024
6d8087a
refactor(x/net/http): Remove debug message and add comments
hackerchai Sep 20, 2024
0382274
neat(x/net/http): Go fmt style
hackerchai Sep 20, 2024
1fb70ad
neat(x/net/http/client): Go fmt style
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=
19 changes: 19 additions & 0 deletions x/net/bytealg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net

func LastIndexByteString(s string, c byte) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == c {
return i
}
}
return -1
}

func IndexByteString(s string, c byte) int {
for i := 0; i < len(s); i++ {
if s[i] == c {
return i
}
}
return -1
}
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 < 100; 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