-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP(x/net/http/client): Implement BodyChunk
- Loading branch information
Showing
16 changed files
with
893 additions
and
784 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func chunkedHandler(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Transfer-Encoding", "chunked") | ||
w.Header().Set("Content-Type", "text/plain") | ||
|
||
flusher, ok := w.(http.Flusher) | ||
if !ok { | ||
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
sentence := "This is a chunked encoded response. It will be sent in multiple parts. Note the delay between each section." | ||
|
||
words := []string{} | ||
start := 0 | ||
for i, r := range sentence { | ||
if r == '。' || r == ',' || i == len(sentence)-1 { | ||
words = append(words, sentence[start:i+1]) | ||
start = i + 1 | ||
} | ||
} | ||
|
||
for _, word := range words { | ||
fmt.Fprintf(w, "%s", word) | ||
flusher.Flush() | ||
} | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/chunked", chunkedHandler) | ||
fmt.Println("Starting server on :8080") | ||
err := http.ListenAndServe(":8080", nil) | ||
if err != nil { | ||
fmt.Printf("Error starting server: %s\n", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"sync" | ||
|
||
"github.com/goplus/llgo/c/libuv" | ||
) | ||
|
||
type onceError struct { | ||
sync.Mutex | ||
err error | ||
} | ||
|
||
func (a *onceError) Store(err error) { | ||
a.Lock() | ||
defer a.Unlock() | ||
if a.err != nil { | ||
return | ||
} | ||
a.err = err | ||
} | ||
|
||
func (a *onceError) Load() error { | ||
a.Lock() | ||
defer a.Unlock() | ||
return a.err | ||
} | ||
|
||
func newBodyChunk(asyncHandle *libuv.Async) *bodyChunk { | ||
return &bodyChunk{ | ||
readCh: make(chan []byte, 1), | ||
done: make(chan struct{}), | ||
asyncHandle: asyncHandle, | ||
} | ||
} | ||
|
||
type bodyChunk struct { | ||
chunk []byte | ||
readCh chan []byte | ||
asyncHandle *libuv.Async | ||
|
||
once sync.Once | ||
done chan struct{} | ||
|
||
rerr onceError | ||
} | ||
|
||
var ( | ||
errClosedBodyChunk = errors.New("bodyChunk: read/write on closed body") | ||
) | ||
|
||
func (bc *bodyChunk) Read(p []byte) (n int, err error) { | ||
for n < len(p) { | ||
if len(bc.chunk) == 0 { | ||
select { | ||
case chunk, ok := <-bc.readCh: | ||
if !ok { | ||
if n > 0 { | ||
return n, nil | ||
} | ||
return 0, bc.readCloseError() | ||
} | ||
bc.chunk = chunk | ||
bc.asyncHandle.Send() | ||
case <-bc.done: | ||
if n > 0 { | ||
return n, nil | ||
} | ||
return 0, io.EOF | ||
} | ||
} | ||
|
||
copied := copy(p[n:], bc.chunk) | ||
n += copied | ||
bc.chunk = bc.chunk[copied:] | ||
} | ||
|
||
return n, nil | ||
} | ||
|
||
func (bc *bodyChunk) Close() error { | ||
return bc.closeRead(nil) | ||
} | ||
|
||
func (bc *bodyChunk) readCloseError() error { | ||
if rerr := bc.rerr.Load(); rerr != nil { | ||
return rerr | ||
} | ||
return errClosedBodyChunk | ||
} | ||
|
||
func (bc *bodyChunk) closeRead(err error) error { | ||
if err == nil { | ||
err = io.EOF | ||
} | ||
bc.rerr.Store(err) | ||
bc.once.Do(func() { | ||
close(bc.done) | ||
}) | ||
//close(bc.done) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.