Skip to content

Commit

Permalink
WIP(x/http/client/get): Extract the readTransfer function and complet…
Browse files Browse the repository at this point in the history
…e its content
  • Loading branch information
spongehah committed Aug 14, 2024
1 parent 2c9394c commit 5744fd6
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 104 deletions.
122 changes: 65 additions & 57 deletions x/http/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/goplus/llgo/c"
"github.com/goplus/llgo/x/textproto"
"github.com/goplus/llgoexamples/rust/hyper"
)

Expand All @@ -18,59 +17,68 @@ type Header map[string][]string
// It appends to any existing values associated with key.
// The key is case insensitive; it is canonicalized by
// CanonicalHeaderKey.
func (h Header) Add(key, value string) {
textproto.MIMEHeader(h).Add(key, value)
}

// Set sets the header entries associated with key to the
// single element value. It replaces any existing values
// associated with key. The key is case insensitive; it is
// canonicalized by textproto.CanonicalMIMEHeaderKey.
// To use non-canonical keys, assign to the map directly.
func (h Header) Set(key, value string) {
textproto.MIMEHeader(h).Set(key, value)
}

// Get gets the first value associated with the given key. If
// there are no values associated with the key, Get returns "".
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
// used to canonicalize the provided key. Get assumes that all
// keys are stored in canonical form. To use non-canonical keys,
// access the map directly.
func (h Header) Get(key string) string {
return textproto.MIMEHeader(h).Get(key)
}

// Values returns all values associated with the given key.
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
// used to canonicalize the provided key. To use non-canonical
// keys, access the map directly.
// The returned slice is not a copy.
func (h Header) Values(key string) []string {
return textproto.MIMEHeader(h).Values(key)
}

// get is like Get, but key must already be in CanonicalHeaderKey form.
func (h Header) get(key string) string {
if v := h[key]; len(v) > 0 {
return v[0]
}
return ""
}

// has reports whether h has the provided key defined, even if it's
// set to 0-length slice.
func (h Header) has(key string) bool {
_, ok := h[key]
return ok
}

// Del deletes the values associated with key.
// The key is case insensitive; it is canonicalized by
// CanonicalHeaderKey.
func (h Header) Del(key string) {
textproto.MIMEHeader(h).Del(key)
}
//func (h Header) Add(key, value string) {

Check warning on line 20 in x/http/header.go

View check run for this annotation

qiniu-x / golangci-lint

x/http/header.go#L20

commentFormatting: put a space between `//` and comment text (gocritic)
// textproto.MIMEHeader(h).Add(key, value)
//}
//
//// Set sets the header entries associated with key to the
//// single element value. It replaces any existing values
//// associated with key. The key is case insensitive; it is
//// canonicalized by textproto.CanonicalMIMEHeaderKey.
//// To use non-canonical keys, assign to the map directly.
//func (h Header) Set(key, value string) {
// textproto.MIMEHeader(h).Set(key, value)
//}
//
//// Get gets the first value associated with the given key. If
//// there are no values associated with the key, Get returns "".
//// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
//// used to canonicalize the provided key. Get assumes that all
//// keys are stored in canonical form. To use non-canonical keys,
//// access the map directly.
//func (h Header) Get(key string) string {
// return textproto.MIMEHeader(h).Get(key)
//}
//
//// Values returns all values associated with the given key.
//// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
//// used to canonicalize the provided key. To use non-canonical
//// keys, access the map directly.
//// The returned slice is not a copy.
//func (h Header) Values(key string) []string {
// return textproto.MIMEHeader(h).Values(key)
//}
//
//// get is like Get, but key must already be in CanonicalHeaderKey form.
//func (h Header) get(key string) string {
// if v := h[key]; len(v) > 0 {
// return v[0]
// }
// return ""
//}
//
//// has reports whether h has the provided key defined, even if it's
//// set to 0-length slice.
//func (h Header) has(key string) bool {
// _, ok := h[key]
// return ok
//}
//
//// Del deletes the values associated with key.
//// The key is case insensitive; it is canonicalized by
//// CanonicalHeaderKey.
//func (h Header) Del(key string) {
// textproto.MIMEHeader(h).Del(key)
//}
//
//// CanonicalHeaderKey returns the canonical format of the
//// header key s. The canonicalization converts the first
//// letter and any letter following a hyphen to upper case;
//// the rest are converted to lowercase. For example, the
//// canonical key for "accept-encoding" is "Accept-Encoding".
//// If s contains a space or invalid header field bytes, it is
//// returned without modifications.
//func CanonicalHeaderKey(s string) string { return textproto.CanonicalMIMEHeaderKey(s) }

// AppendToResponseHeader (HeadersForEachCallback) prints each header to the console

Check warning on line 83 in x/http/header.go

View check run for this annotation

qiniu-x / golangci-lint

x/http/header.go#L83

Comment should end in a period (godot)
func AppendToResponseHeader(userdata c.Pointer, name *uint8, nameLen uintptr, value *uint8, valueLen uintptr) c.Int {
Expand All @@ -79,10 +87,10 @@ func AppendToResponseHeader(userdata c.Pointer, name *uint8, nameLen uintptr, va
valueStr := string((*[1 << 30]byte)(c.Pointer(value))[:valueLen:valueLen])

if resp.Header == nil {
resp.Header = make(map[string][]string)
resp.Header = make(Header)
}
resp.Header.Add(nameStr, valueStr)
//resp.Header[nameStr] = append(resp.Header[nameStr], valueStr)
//resp.Header.Add(nameStr, valueStr)

Check warning on line 92 in x/http/header.go

View check run for this annotation

qiniu-x / golangci-lint

x/http/header.go#L92

commentFormatting: put a space between `//` and comment text (gocritic)
resp.Header[nameStr] = append(resp.Header[nameStr], valueStr)
return hyper.IterContinue
}

Expand Down
3 changes: 2 additions & 1 deletion x/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
Header: make(Header),
timeout: 0,
}
request.Header.Set("Host", request.Host)
//request.Header.Set("Host", request.Host)

Check warning on line 39 in x/http/request.go

View check run for this annotation

qiniu-x / golangci-lint

x/http/request.go#L39

commentFormatting: put a space between `//` and comment text (gocritic)
request.Header["Host"] = []string{request.Host}
return request, nil
}

Expand Down
30 changes: 28 additions & 2 deletions x/http/response.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
package http

import (
"fmt"
"io"
"strconv"

"github.com/goplus/llgo/c"
"github.com/goplus/llgoexamples/rust/hyper"
)

type Response struct {
Status string
StatusCode int
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
ProtoMajor int // e.g. 1
ProtoMinor int // e.g. 0
Header Header
Body io.ReadCloser
ContentLength int64
Trailer Header
Chunked bool
Request *Request
}

func readResponseLineAndHeader(resp *Response, hyperResp *hyper.Response) {
rp := hyperResp.ReasonPhrase()
rpLen := hyperResp.ReasonPhraseLen()

resp.Status = strconv.Itoa(int(hyperResp.Status())) + " " + string((*[1 << 30]byte)(c.Pointer(rp))[:rpLen:rpLen])
resp.StatusCode = int(hyperResp.Status())

version := int(hyperResp.Version())
resp.ProtoMajor, resp.ProtoMinor = splitTwoDigitNumber(version)
resp.Proto = fmt.Sprintf("HTTP/%d.%d", resp.ProtoMajor, resp.ProtoMinor)

headers := hyperResp.Headers()
headers.Foreach(AppendToResponseHeader, c.Pointer(resp))
}
Loading

0 comments on commit 5744fd6

Please sign in to comment.