comment updates for package ghttp

This commit is contained in:
John
2020-02-11 10:00:10 +08:00
parent 88684ca00a
commit 78917ed5cb
3 changed files with 12 additions and 10 deletions

View File

@ -7,18 +7,19 @@
package ghttp
import (
"github.com/gogf/gf/util/gconv"
"io/ioutil"
"net/http"
"time"
)
// 客户端请求结果对象
// ClientResponse is the struct for client request response.
type ClientResponse struct {
*http.Response
cookies map[string]string
}
// 获得返回的指定COOKIE值
// GetCookie retrieves and returns the cookie value of specified <key>.
func (r *ClientResponse) GetCookie(key string) string {
if len(r.cookies) == 0 {
now := time.Now()
@ -32,7 +33,7 @@ func (r *ClientResponse) GetCookie(key string) string {
return r.cookies[key]
}
// 获取返回的数据(二进制).
// ReadAll retrieves and returns the response content as []byte.
func (r *ClientResponse) ReadAll() []byte {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
@ -41,12 +42,12 @@ func (r *ClientResponse) ReadAll() []byte {
return body
}
// 获取返回的数据(字符串).
// ReadAllString retrieves and returns the response content as string.
func (r *ClientResponse) ReadAllString() string {
return string(r.ReadAll())
return gconv.UnsafeBytesToStr(r.ReadAll())
}
// 关闭返回的HTTP链接
// Close closes the response when it will never be used.
func (r *ClientResponse) Close() error {
r.Response.Close = true
return r.Body.Close()

View File

@ -6,7 +6,7 @@
package ghttp
// 控制器接口
// Controller is the base struct for controller.
type Controller interface {
Init(*Request)
Shut()

View File

@ -14,7 +14,7 @@ import (
"github.com/gogf/gf/encoding/gbase64"
)
// 设置Basic Auth校验提示
// setBasicAuth sets the http basic authentication tips.
func (r *Request) setBasicAuth(tips ...string) {
realm := ""
if len(tips) > 0 && tips[0] != "" {
@ -26,8 +26,9 @@ func (r *Request) setBasicAuth(tips ...string) {
r.Response.WriteHeader(http.StatusUnauthorized)
}
// 设置HTTP基础账号密码认证如果用户没有提交账号密码那么提示用户输出信息。
// 验证成功之后返回true否则返回false。
// BasicAuth enables the http basic authentication feature with given passport and password
// and asks client for authentication. It returns true if authentication success, else returns
// false if failure.
func (r *Request) BasicAuth(user, pass string, tips ...string) bool {
auth := r.Header.Get("Authorization")
if auth == "" {