github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/standard/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 standard
    19  
    20  import (
    21  	"bufio"
    22  	"github.com/aacfactory/errors"
    23  	"github.com/aacfactory/fns/commons/bytex"
    24  	"github.com/aacfactory/fns/context"
    25  	"github.com/aacfactory/fns/transports"
    26  	"net"
    27  	"net/http"
    28  	"time"
    29  )
    30  
    31  type ResponseWriter struct {
    32  	context.Context
    33  	writer   http.ResponseWriter
    34  	header   transports.Header
    35  	result   *transports.ResultResponseWriter
    36  	hijacked bool
    37  }
    38  
    39  func (w *ResponseWriter) Status() int {
    40  	return w.result.Status()
    41  }
    42  
    43  func (w *ResponseWriter) SetStatus(status int) {
    44  	w.result.SetStatus(status)
    45  }
    46  
    47  func (w *ResponseWriter) SetCookie(cookie *transports.Cookie) {
    48  	c := http.Cookie{
    49  		Name:       bytex.ToString(cookie.Key()),
    50  		Value:      bytex.ToString(cookie.Value()),
    51  		Path:       bytex.ToString(cookie.Path()),
    52  		Domain:     bytex.ToString(cookie.Domain()),
    53  		Expires:    cookie.Expire(),
    54  		RawExpires: "",
    55  		MaxAge:     cookie.MaxAge(),
    56  		Secure:     cookie.Secure(),
    57  		HttpOnly:   cookie.HTTPOnly(),
    58  		SameSite:   http.SameSite(cookie.SameSite()),
    59  		Raw:        "",
    60  		Unparsed:   nil,
    61  	}
    62  	http.SetCookie(w.writer, &c)
    63  }
    64  
    65  func (w *ResponseWriter) Header() transports.Header {
    66  	return w.header
    67  }
    68  
    69  func (w *ResponseWriter) Succeed(v interface{}) {
    70  	w.result.Succeed(v)
    71  	return
    72  }
    73  
    74  func (w *ResponseWriter) Failed(cause error) {
    75  	w.result.Failed(cause)
    76  	return
    77  }
    78  
    79  func (w *ResponseWriter) Write(body []byte) (int, error) {
    80  	return w.result.Write(body)
    81  }
    82  
    83  func (w *ResponseWriter) Body() []byte {
    84  	return w.result.Body()
    85  }
    86  
    87  func (w *ResponseWriter) BodyLen() int {
    88  	return w.result.BodyLen()
    89  }
    90  
    91  func (w *ResponseWriter) ResetBody() {
    92  	w.result.ResetBody()
    93  }
    94  
    95  func (w *ResponseWriter) Hijack(f func(ctx context.Context, conn net.Conn, rw *bufio.ReadWriter) (err error)) (async bool, err error) {
    96  	if f == nil {
    97  		err = errors.Warning("fns: hijack function is nil")
    98  		return
    99  	}
   100  	h, ok := w.writer.(http.Hijacker)
   101  	if !ok {
   102  		err = errors.Warning("fns: connection can not be hijacked")
   103  		return
   104  	}
   105  	conn, brw, hijackErr := h.Hijack()
   106  	if hijackErr != nil {
   107  		err = errors.Warning("fns: connection hijack failed").WithCause(hijackErr)
   108  		return
   109  	}
   110  	w.hijacked = true
   111  	err = f(w.Context, conn, brw)
   112  	return
   113  }
   114  
   115  func (w *ResponseWriter) Hijacked() bool {
   116  	return w.hijacked
   117  }
   118  
   119  func (w *ResponseWriter) WriteTimeout() time.Duration {
   120  	return w.result.WriteTimeout()
   121  }
   122  
   123  func (w *ResponseWriter) WriteDeadline() time.Time {
   124  	return w.result.WriteDeadline()
   125  }