Files
drip/internal/shared/httputil/response.go
zhiqing 307cf8e6cc feat: Add Bearer Token authentication support and optimize code structure
- Add Bearer Token authentication, supporting tunnel access control via the --auth-bearer parameter
- Refactor large modules into smaller, more focused components to improve code maintainability
- Update dependency versions, including golang.org/x/crypto, golang.org/x/net, etc.
- Add SilenceUsage and SilenceErrors configuration for all CLI commands
- Modify connector configuration structure to support the new authentication method
- Update recent change log in README with new feature descriptions

BREAKING CHANGE: Authentication via Bearer Token is now supported, requiring the new --auth-bearer parameter
2026-01-29 14:40:53 +08:00

39 lines
1.2 KiB
Go

package httputil
import (
"fmt"
"net/http"
)
// WriteJSON writes a JSON response with the appropriate headers.
func WriteJSON(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Write(data)
}
// WriteHTML writes an HTML response with the appropriate headers.
func WriteHTML(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Write(data)
}
// WriteHTMLWithStatus writes an HTML response with a custom status code.
func WriteHTMLWithStatus(w http.ResponseWriter, data []byte, statusCode int) {
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.WriteHeader(statusCode)
w.Write(data)
}
// SetContentLength sets the Content-Length header.
func SetContentLength(w http.ResponseWriter, length int64) {
w.Header().Set("Content-Length", fmt.Sprintf("%d", length))
}
// SetCloseConnection sets the Connection: close header.
func SetCloseConnection(w http.ResponseWriter) {
w.Header().Set("Connection", "close")
}