github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/gateway/tcp/conn_http.go (about)

     1  package tcpGateway
     2  
     3  import (
     4  	"mime/multipart"
     5  
     6  	"github.com/ronaksoft/rony/internal/metrics"
     7  	"github.com/ronaksoft/rony/tools"
     8  	"github.com/valyala/fasthttp"
     9  )
    10  
    11  /*
    12     Creation Time: 2019 - Nov - 28
    13     Created by:  (ehsan)
    14     Maintainers:
    15        1.  Ehsan N. Moosa (E2)
    16     Auditor: Ehsan N. Moosa (E2)
    17     Copyright Ronak Software Group 2020
    18  */
    19  
    20  var (
    21  	strLocation = []byte(fasthttp.HeaderLocation)
    22  )
    23  
    24  // httpConn
    25  type httpConn struct {
    26  	gateway    *Gateway
    27  	ctx        *fasthttp.RequestCtx
    28  	clientIP   []byte
    29  	clientType []byte
    30  	mtx        tools.SpinLock
    31  	kv         map[string]interface{}
    32  }
    33  
    34  func (c *httpConn) Get(key string) interface{} {
    35  	c.mtx.Lock()
    36  	v := c.kv[key]
    37  	c.mtx.Unlock()
    38  
    39  	return v
    40  }
    41  
    42  func (c *httpConn) Set(key string, val interface{}) {
    43  	c.mtx.Lock()
    44  	c.kv[key] = val
    45  	c.mtx.Unlock()
    46  }
    47  
    48  func (c *httpConn) Walk(f func(k string, v interface{}) bool) {
    49  	c.mtx.Lock()
    50  	defer c.mtx.Unlock()
    51  
    52  	for k, v := range c.kv {
    53  		if !f(k, v) {
    54  			return
    55  		}
    56  	}
    57  }
    58  
    59  func (c *httpConn) ConnID() uint64 {
    60  	return c.ctx.ConnID()
    61  }
    62  
    63  func (c *httpConn) ClientIP() string {
    64  	return string(c.clientIP)
    65  }
    66  
    67  func (c *httpConn) SetClientIP(ip []byte) {
    68  	c.clientIP = append(c.clientIP[:0], ip...)
    69  }
    70  
    71  func (c *httpConn) SetClientType(ct []byte) {
    72  	c.clientType = append(c.clientType[:0], ct...)
    73  }
    74  
    75  func (c *httpConn) WriteStatus(status int) {
    76  	c.ctx.SetStatusCode(status)
    77  }
    78  
    79  func (c *httpConn) ReadHeader(key string) string {
    80  	return string(c.ctx.Request.Header.Peek(key))
    81  }
    82  
    83  func (c *httpConn) WriteBinary(streamID int64, data []byte) (err error) {
    84  	_, err = c.ctx.Write(data)
    85  	metrics.IncCounter(metrics.CntGatewayOutgoingHttpMessage)
    86  
    87  	return err
    88  }
    89  
    90  func (c *httpConn) WriteHeader(key, value string) {
    91  	c.ctx.Response.Header.Add(key, value)
    92  }
    93  
    94  func (c *httpConn) MultiPart() (*multipart.Form, error) {
    95  	return c.ctx.MultipartForm()
    96  }
    97  
    98  func (c *httpConn) RequestURI() string {
    99  	return tools.B2S(c.ctx.Request.Header.RequestURI())
   100  }
   101  
   102  func (c *httpConn) Schema() string {
   103  	return tools.B2S(c.ctx.Request.URI().Scheme())
   104  }
   105  
   106  func (c *httpConn) Host() string {
   107  	return tools.B2S(c.ctx.Request.URI().Host())
   108  }
   109  
   110  func (c *httpConn) Method() string {
   111  	return tools.B2S(c.ctx.Method())
   112  }
   113  
   114  func (c *httpConn) Path() string {
   115  	return tools.B2S(c.ctx.Request.URI().PathOriginal())
   116  }
   117  
   118  func (c *httpConn) Body() []byte {
   119  	return c.ctx.PostBody()
   120  }
   121  
   122  func (c *httpConn) Persistent() bool {
   123  	return false
   124  }
   125  
   126  func (c *httpConn) Redirect(statusCode int, newHostPort string) {
   127  	u := fasthttp.AcquireURI()
   128  	c.ctx.URI().CopyTo(u)
   129  	u.SetHost(newHostPort)
   130  	c.ctx.Response.Header.SetCanonical(strLocation, u.FullURI())
   131  	c.ctx.Response.SetStatusCode(statusCode)
   132  	fasthttp.ReleaseURI(u)
   133  }
   134  
   135  func (c *httpConn) RedirectURL(statusCode int, url string) {
   136  	u := fasthttp.AcquireURI()
   137  	_ = u.Parse(nil, tools.S2B(url))
   138  	c.ctx.Response.Header.SetCanonical(strLocation, u.FullURI())
   139  	c.ctx.Response.SetStatusCode(statusCode)
   140  	fasthttp.ReleaseURI(u)
   141  }