github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/response_writer.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package safehttp
    16  
    17  // ResponseWriter is used to construct an HTTP response. When a Response is
    18  // passed to the ResponseWriter, it will invoke the Dispatcher with the
    19  // Response. An attempt to write to the ResponseWriter twice will
    20  // cause a panic.
    21  //
    22  // A ResponseWriter may not be used after the Handler.ServeHTTP method has returned.
    23  type ResponseWriter interface {
    24  	ResponseHeadersWriter
    25  
    26  	// Write writes a safe response.
    27  	Write(resp Response) Result
    28  
    29  	// WriteError writes an error response (400-599).
    30  	//
    31  	// If the ResponseWriter has already been written to, then this method panics.
    32  	WriteError(resp ErrorResponse) Result
    33  }
    34  
    35  // ResponseHeadersWriter is used to alter the HTTP response headers.
    36  //
    37  // A ResponseHeadersWriter may not be used after the Handler.ServeHTTP method has returned.
    38  type ResponseHeadersWriter interface {
    39  	// Header returns the collection of headers that will be set
    40  	// on the response. Headers must be set before writing a
    41  	// response (e.g. Write, WriteTemplate).
    42  	Header() Header
    43  
    44  	// AddCookie adds a Set-Cookie header to the provided ResponseWriter's headers.
    45  	// The provided cookie must have a valid Name, otherwise an error will be
    46  	// returned.
    47  	AddCookie(c *Cookie) error
    48  }