github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/fast/response.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package fast
    19  
    20  import (
    21  	"bufio"
    22  	"github.com/aacfactory/errors"
    23  	"github.com/aacfactory/fns/context"
    24  	"github.com/aacfactory/fns/transports"
    25  	"github.com/valyala/fasthttp"
    26  	"net"
    27  	"time"
    28  )
    29  
    30  type ResponseWriter struct {
    31  	*Context
    32  	result *transports.ResultResponseWriter
    33  }
    34  
    35  func (w *ResponseWriter) Status() int {
    36  	return w.result.Status()
    37  }
    38  
    39  func (w *ResponseWriter) SetStatus(status int) {
    40  	w.result.SetStatus(status)
    41  }
    42  
    43  func (w *ResponseWriter) SetCookie(cookie *transports.Cookie) {
    44  	c := fasthttp.AcquireCookie()
    45  	defer fasthttp.ReleaseCookie(c)
    46  	c.SetKeyBytes(cookie.Key())
    47  	c.SetValueBytes(cookie.Value())
    48  	c.SetExpire(cookie.Expire())
    49  	c.SetMaxAge(cookie.MaxAge())
    50  	c.SetDomainBytes(cookie.Domain())
    51  	c.SetPathBytes(cookie.Path())
    52  	c.SetHTTPOnly(cookie.HTTPOnly())
    53  	c.SetSecure(cookie.Secure())
    54  	c.SetSameSite(fasthttp.CookieSameSite(cookie.SameSite()))
    55  	w.Context.Response.Header.SetCookie(c)
    56  }
    57  
    58  func (w *ResponseWriter) Header() transports.Header {
    59  	return &ResponseHeader{
    60  		&w.Context.Response.Header,
    61  	}
    62  }
    63  
    64  func (w *ResponseWriter) Succeed(v interface{}) {
    65  	w.result.Succeed(v)
    66  	return
    67  }
    68  
    69  func (w *ResponseWriter) Failed(cause error) {
    70  	w.result.Failed(cause)
    71  	return
    72  }
    73  
    74  func (w *ResponseWriter) Write(body []byte) (int, error) {
    75  	return w.result.Write(body)
    76  }
    77  
    78  func (w *ResponseWriter) Body() []byte {
    79  	return w.result.Body()
    80  }
    81  
    82  func (w *ResponseWriter) BodyLen() int {
    83  	return w.result.BodyLen()
    84  }
    85  
    86  func (w *ResponseWriter) ResetBody() {
    87  	w.result.ResetBody()
    88  }
    89  
    90  func (w *ResponseWriter) Hijack(f func(ctx context.Context, conn net.Conn, rw *bufio.ReadWriter) (err error)) (async bool, err error) {
    91  	if f == nil {
    92  		err = errors.Warning("fns: hijack function is nil")
    93  		return
    94  	}
    95  	w.Context.Hijack(func(c net.Conn) {
    96  		_ = f(w, c, nil)
    97  	})
    98  	async = true
    99  	return
   100  }
   101  
   102  func (w *ResponseWriter) Hijacked() bool {
   103  	return w.Context.Hijacked()
   104  }
   105  
   106  func (w *ResponseWriter) WriteTimeout() time.Duration {
   107  	return w.result.WriteTimeout()
   108  }
   109  
   110  func (w *ResponseWriter) WriteDeadline() time.Time {
   111  	return w.result.WriteDeadline()
   112  }