Skip to content

Commit

Permalink
feat: add net/http (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou authored Feb 3, 2023
1 parent f77e2d0 commit 7054838
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 54 deletions.
27 changes: 1 addition & 26 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
version: [ 1.18, 1.19 ]
runs-on: [ self-hosted, X64 ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

Expand All @@ -29,28 +29,3 @@ jobs:
version: latest
- name: Unit Test
run: go test -race -covermode=atomic -coverprofile=coverage.txt ./...

- name: Codecov
run: bash <(curl -s https://codecov.io/bash)

ut-windows:
strategy:
matrix:
version: [ 1.18, 1.19 ]
runs-on: windows-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.version }}

- uses: actions/cache@v3
with:
path: $HOME/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Unit Test
run: go test -race -covermode=atomic ./...
33 changes: 26 additions & 7 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package main

import (
"bytes"
"fmt"
"net/http"

"github.com/valyala/fasthttp"

"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/li-jin-gou/http2curl"
"github.com/valyala/fasthttp"
)

func main() {
HertzDemo()
FastHttpDemo()
NetHttpDemo()
}

func HertzDemo() {
// hertz
req := protocol.NewRequest(consts.MethodGet, "https://example.com/index", nil)
req.URI().QueryArgs().Add("a", "1")
Expand All @@ -18,15 +27,25 @@ func main() {
c, _ := http2curl.GetCurlCommandHertz(req)
fmt.Println(c)
// Output: curl -k -X 'GET' -H 'A: 2' -H 'Host: example.com' 'https://example.com/index?a=1&b=2' --compressed
}

func FastHttpDemo() {
// fasthttp
var req1 fasthttp.Request
req1.SetRequestURI("https://example.com/index")
req1.Header.SetMethod(fasthttp.MethodPost)
req1.SetBody([]byte(`{"a":"b"}`))
req1.Header.Set("Content-Type", "application/json")
var req fasthttp.Request
req.SetRequestURI("https://example.com/index")
req.Header.SetMethod(fasthttp.MethodPost)
req.SetBody([]byte(`{"a":"b"}`))
req.Header.Set("Content-Type", "application/json")

c, _ := http2curl.GetCurlCommandFastHttp(&req)
fmt.Println(c)
// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed
}

c, _ = http2curl.GetCurlCommandFastHttp(&req1)
func NetHttpDemo() {
req, _ := http.NewRequest(http.MethodPost, "https://example.com/index", bytes.NewBufferString(`{"a":"b"}`))
req.Header.Set("Content-Type", "application/json")
c, _ := http2curl.GetCurlCommand(req)
fmt.Println(c)
// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed
}
30 changes: 30 additions & 0 deletions htt2curl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package http2curl

import (
"bytes"
"net/http"
"testing"

"github.com/cloudwego/hertz/pkg/common/test/assert"
)

func TestGetCurlCommand(t *testing.T) {
t.Parallel()

t.Run("test1", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://example.com/index?a=1&b=2", nil)
req.Header.Set("a", "2")
c, err := GetCurlCommand(req)
assert.DeepEqual(t, nil, err)
assert.DeepEqual(t, "curl -k -X 'GET' -H 'A: 2' 'https://example.com/index?a=1&b=2' --compressed", c.String())
})

t.Run("test2", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "https://example.com/index", bytes.NewBufferString(`{"a":"b"}`))
req.Header.Set("Content-Type", "application/json")

command, err := GetCurlCommand(req)
assert.DeepEqual(t, nil, err)
assert.DeepEqual(t, "curl -k -X 'POST' -d '{\"a\":\"b\"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed", command.String())
})
}
63 changes: 63 additions & 0 deletions http2curl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package http2curl

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"sort"
"strings"
"unsafe"
)
Expand Down Expand Up @@ -34,3 +39,61 @@ func b2s(b []byte) string {
/* #nosec G103 */
return *(*string)(unsafe.Pointer(&b))
}

// GetCurlCommand returns a CurlCommand corresponding to an http.Request
func GetCurlCommand(req *http.Request) (*CurlCommand, error) {
if req.URL == nil {
return nil, ErrorURINull
}

command := CurlCommand{}

command.append("curl")

schema := req.URL.Scheme
requestURL := req.URL.String()
if schema == "" {
schema = "http"
if req.TLS != nil {
schema = "https"
}
requestURL = schema + "://" + req.Host + req.URL.Path
}

if schema == "https" {
command.append("-k")
}

command.append("-X", bashEscape(req.Method))

if req.Body != nil {
var buff bytes.Buffer
_, err := buff.ReadFrom(req.Body)
if err != nil {
return nil, fmt.Errorf("getCurlCommand: buffer read from body error: %w", err)
}
// reset body for potential re-reads
req.Body = io.NopCloser(bytes.NewBuffer(buff.Bytes()))
if len(buff.String()) > 0 {
bodyEscaped := bashEscape(buff.String())
command.append("-d", bodyEscaped)
}
}

var keys []string

for k := range req.Header {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
command.append("-H", bashEscape(fmt.Sprintf("%s: %s", k, strings.Join(req.Header[k], " "))))
}

command.append(bashEscape(requestURL))

command.append("--compressed")

return &command, nil
}
2 changes: 1 addition & 1 deletion http2curl_fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/valyala/fasthttp"
)

// GetCurlCommandFastHttp returns a CurlCommand corresponding to an http.Request
// GetCurlCommandFastHttp returns a CurlCommand corresponding to an fasthttp.Request
func GetCurlCommandFastHttp(req *fasthttp.Request) (*CurlCommand, error) {
if req.URI() == nil {
return nil, ErrorURINull
Expand Down
2 changes: 1 addition & 1 deletion http2curl_hertz.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/cloudwego/hertz/pkg/protocol"
)

// GetCurlCommandHertz returns a CurlCommand corresponding to an http.Request
// GetCurlCommandHertz returns a CurlCommand corresponding to an protocol.Request
func GetCurlCommandHertz(req *protocol.Request) (*CurlCommand, error) {
if req.URI() == nil {
return nil, ErrorURINull
Expand Down
48 changes: 29 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# http2curl

convert Request of [fasthttp](https://github.com/valyala/fasthttp) and [hertz](https://github.com/cloudwego/hertz) to CURL command line and fork from [moul/http2curl](https://github.com/moul/http2curl)
convert Request of [fasthttp](https://github.com/valyala/fasthttp), [hertz](https://github.com/cloudwego/hertz) and net/http to CURL command line and fork from [moul/http2curl](https://github.com/moul/http2curl)


# Install
Expand All @@ -9,21 +9,31 @@ convert Request of [fasthttp](https://github.com/valyala/fasthttp) and [hertz](h
go get github.com/li-jin-gou/http2curl
```

## Example
## Usage


### FastHttp

```go
package main
func FastHttpDemo() {
// fasthttp
var req fasthttp.Request
req.SetRequestURI("https://example.com/index")
req.Header.SetMethod(fasthttp.MethodPost)
req.SetBody([]byte(`{"a":"b"}`))
req.Header.Set("Content-Type", "application/json")

c, _ := http2curl.GetCurlCommandFastHttp(&req)
fmt.Println(c)
// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed
}

import (
"fmt"
```

"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/li-jin-gou/http2curl"
"github.com/valyala/fasthttp"
)
### Hertz

func main() {
```go
func HertzDemo() {
// hertz
req := protocol.NewRequest(consts.MethodGet, "https://example.com/index", nil)
req.URI().QueryArgs().Add("a", "1")
Expand All @@ -32,17 +42,17 @@ func main() {
c, _ := http2curl.GetCurlCommandHertz(req)
fmt.Println(c)
// Output: curl -k -X 'GET' -H 'A: 2' -H 'Host: example.com' 'https://example.com/index?a=1&b=2' --compressed
}
```

// fasthttp
var req1 fasthttp.Request
req1.SetRequestURI("https://example.com/index")
req1.Header.SetMethod(fasthttp.MethodPost)
req1.SetBody([]byte(`{"a":"b"}`))
req1.Header.Set("Content-Type", "application/json")
### net/http

c, _ = http2curl.GetCurlCommandFastHttp(&req1)
```go
func NetHttpDemo() {
req, _ := http.NewRequest(http.MethodPost, "https://example.com/index", bytes.NewBufferString(`{"a":"b"}`))
req.Header.Set("Content-Type", "application/json")
c, _ := http2curl.GetCurlCommand(req)
fmt.Println(c)
// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed
}

```

0 comments on commit 7054838

Please sign in to comment.