github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_response_write.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  //
     7  
     8  package ghttp
     9  
    10  import (
    11  	"fmt"
    12  	"net/http"
    13  
    14  	"github.com/gogf/gf/v2/encoding/gjson"
    15  	"github.com/gogf/gf/v2/errors/gerror"
    16  	"github.com/gogf/gf/v2/internal/json"
    17  	"github.com/gogf/gf/v2/util/gconv"
    18  )
    19  
    20  // Write writes `content` to the response buffer.
    21  func (r *Response) Write(content ...interface{}) {
    22  	if r.IsHijacked() || len(content) == 0 {
    23  		return
    24  	}
    25  	if r.Status == 0 {
    26  		r.Status = http.StatusOK
    27  	}
    28  	for _, v := range content {
    29  		switch value := v.(type) {
    30  		case []byte:
    31  			_, _ = r.BufferWriter.Write(value)
    32  		case string:
    33  			_, _ = r.BufferWriter.WriteString(value)
    34  		default:
    35  			_, _ = r.BufferWriter.WriteString(gconv.String(v))
    36  		}
    37  	}
    38  }
    39  
    40  // WriteExit writes `content` to the response buffer and exits executing of current handler.
    41  // The "Exit" feature is commonly used to replace usage of return statements in the handler,
    42  // for convenience.
    43  func (r *Response) WriteExit(content ...interface{}) {
    44  	r.Write(content...)
    45  	r.Request.Exit()
    46  }
    47  
    48  // WriteOver overwrites the response buffer with `content`.
    49  func (r *Response) WriteOver(content ...interface{}) {
    50  	r.ClearBuffer()
    51  	r.Write(content...)
    52  }
    53  
    54  // WriteOverExit overwrites the response buffer with `content` and exits executing
    55  // of current handler. The "Exit" feature is commonly used to replace usage of return
    56  // statements in the handler, for convenience.
    57  func (r *Response) WriteOverExit(content ...interface{}) {
    58  	r.WriteOver(content...)
    59  	r.Request.Exit()
    60  }
    61  
    62  // Writef writes the response with fmt.Sprintf.
    63  func (r *Response) Writef(format string, params ...interface{}) {
    64  	r.Write(fmt.Sprintf(format, params...))
    65  }
    66  
    67  // WritefExit writes the response with fmt.Sprintf and exits executing of current handler.
    68  // The "Exit" feature is commonly used to replace usage of return statements in the handler,
    69  // for convenience.
    70  func (r *Response) WritefExit(format string, params ...interface{}) {
    71  	r.Writef(format, params...)
    72  	r.Request.Exit()
    73  }
    74  
    75  // Writeln writes the response with `content` and new line.
    76  func (r *Response) Writeln(content ...interface{}) {
    77  	if len(content) == 0 {
    78  		r.Write("\n")
    79  		return
    80  	}
    81  	r.Write(append(content, "\n")...)
    82  }
    83  
    84  // WritelnExit writes the response with `content` and new line and exits executing
    85  // of current handler. The "Exit" feature is commonly used to replace usage of return
    86  // statements in the handler, for convenience.
    87  func (r *Response) WritelnExit(content ...interface{}) {
    88  	r.Writeln(content...)
    89  	r.Request.Exit()
    90  }
    91  
    92  // Writefln writes the response with fmt.Sprintf and new line.
    93  func (r *Response) Writefln(format string, params ...interface{}) {
    94  	r.Writeln(fmt.Sprintf(format, params...))
    95  }
    96  
    97  // WriteflnExit writes the response with fmt.Sprintf and new line and exits executing
    98  // of current handler. The "Exit" feature is commonly used to replace usage of return
    99  // statement in the handler, for convenience.
   100  func (r *Response) WriteflnExit(format string, params ...interface{}) {
   101  	r.Writefln(format, params...)
   102  	r.Request.Exit()
   103  }
   104  
   105  // WriteJson writes `content` to the response with JSON format.
   106  func (r *Response) WriteJson(content interface{}) {
   107  	r.Header().Set("Content-Type", contentTypeJson)
   108  	// If given string/[]byte, response it directly to the client.
   109  	switch content.(type) {
   110  	case string, []byte:
   111  		r.Write(gconv.String(content))
   112  		return
   113  	}
   114  	// Else use json.Marshal function to encode the parameter.
   115  	if b, err := json.Marshal(content); err != nil {
   116  		panic(gerror.Wrap(err, `WriteJson failed`))
   117  	} else {
   118  		r.Write(b)
   119  	}
   120  }
   121  
   122  // WriteJsonExit writes `content` to the response with JSON format and exits executing
   123  // of current handler if success. The "Exit" feature is commonly used to replace usage of
   124  // return statements in the handler, for convenience.
   125  func (r *Response) WriteJsonExit(content interface{}) {
   126  	r.WriteJson(content)
   127  	r.Request.Exit()
   128  }
   129  
   130  // WriteJsonP writes `content` to the response with JSONP format.
   131  //
   132  // Note that there should be a "callback" parameter in the request for JSONP format.
   133  func (r *Response) WriteJsonP(content interface{}) {
   134  	r.Header().Set("Content-Type", contentTypeJavascript)
   135  	// If given string/[]byte, response it directly to client.
   136  	switch content.(type) {
   137  	case string, []byte:
   138  		r.Write(gconv.String(content))
   139  		return
   140  	}
   141  	// Else use json.Marshal function to encode the parameter.
   142  	if b, err := json.Marshal(content); err != nil {
   143  		panic(gerror.Wrap(err, `WriteJsonP failed`))
   144  	} else {
   145  		// r.Header().Set("Content-Type", "application/json")
   146  		if callback := r.Request.Get("callback").String(); callback != "" {
   147  			buffer := []byte(callback)
   148  			buffer = append(buffer, byte('('))
   149  			buffer = append(buffer, b...)
   150  			buffer = append(buffer, byte(')'))
   151  			r.Write(buffer)
   152  		} else {
   153  			r.Write(b)
   154  		}
   155  	}
   156  }
   157  
   158  // WriteJsonPExit writes `content` to the response with JSONP format and exits executing
   159  // of current handler if success. The "Exit" feature is commonly used to replace usage of
   160  // return statements in the handler, for convenience.
   161  //
   162  // Note that there should be a "callback" parameter in the request for JSONP format.
   163  func (r *Response) WriteJsonPExit(content interface{}) {
   164  	r.WriteJsonP(content)
   165  	r.Request.Exit()
   166  }
   167  
   168  // WriteXml writes `content` to the response with XML format.
   169  func (r *Response) WriteXml(content interface{}, rootTag ...string) {
   170  	r.Header().Set("Content-Type", contentTypeXml)
   171  	// If given string/[]byte, response it directly to clients.
   172  	switch content.(type) {
   173  	case string, []byte:
   174  		r.Write(gconv.String(content))
   175  		return
   176  	}
   177  	if b, err := gjson.New(content).ToXml(rootTag...); err != nil {
   178  		panic(gerror.Wrap(err, `WriteXml failed`))
   179  	} else {
   180  		r.Write(b)
   181  	}
   182  }
   183  
   184  // WriteXmlExit writes `content` to the response with XML format and exits executing
   185  // of current handler if success. The "Exit" feature is commonly used to replace usage
   186  // of return statements in the handler, for convenience.
   187  func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) {
   188  	r.WriteXml(content, rootTag...)
   189  	r.Request.Exit()
   190  }
   191  
   192  // WriteStatus writes HTTP `status` and `content` to the response.
   193  // Note that it does not set a Content-Type header here.
   194  func (r *Response) WriteStatus(status int, content ...interface{}) {
   195  	r.WriteHeader(status)
   196  	if len(content) > 0 {
   197  		r.Write(content...)
   198  	} else {
   199  		r.Write(http.StatusText(status))
   200  	}
   201  }
   202  
   203  // WriteStatusExit writes HTTP `status` and `content` to the response and exits executing
   204  // of current handler if success. The "Exit" feature is commonly used to replace usage of
   205  // return statements in the handler, for convenience.
   206  func (r *Response) WriteStatusExit(status int, content ...interface{}) {
   207  	r.WriteStatus(status, content...)
   208  	r.Request.Exit()
   209  }