Compare commits
No commits in common. "472d17509315e5d448c7e85787bbfa9c3d55c575" and "d516147238b4bddb060c071f92908d99bce80caf" have entirely different histories.
472d175093
...
d516147238
|
@ -3,7 +3,6 @@ package http
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt" // Added for fmt.Fprintf
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
@ -280,37 +279,21 @@ func writeResponse(w http.ResponseWriter, result any) {
|
|||
result = httpResp.Body // Set result to body for processing below
|
||||
}
|
||||
|
||||
// Check if it's a map (table) or array - return as JSON
|
||||
isJSON := false
|
||||
|
||||
switch result.(type) {
|
||||
case map[string]any, []any, []float64, []string, []int:
|
||||
isJSON = true
|
||||
}
|
||||
|
||||
if isJSON {
|
||||
switch res := result.(type) {
|
||||
case string:
|
||||
// String result - default to plain text
|
||||
setContentTypeIfMissing(w, contentTypePlain)
|
||||
w.Write([]byte(res))
|
||||
default:
|
||||
// All other types - convert to JSON
|
||||
setContentTypeIfMissing(w, contentTypeJSON)
|
||||
data, err := json.Marshal(result)
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
logger.Error("Failed to marshal response: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
// All other types - convert to plain text
|
||||
setContentTypeIfMissing(w, contentTypePlain)
|
||||
|
||||
switch r := result.(type) {
|
||||
case string:
|
||||
w.Write([]byte(r))
|
||||
case []byte:
|
||||
w.Write(r)
|
||||
default:
|
||||
// Convert any other type to string
|
||||
fmt.Fprintf(w, "%v", r)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -117,22 +117,23 @@ local cookie = {
|
|||
if opts.expires then
|
||||
if type(opts.expires) == "number" then
|
||||
if opts.expires > 0 then
|
||||
-- Add seconds to current time
|
||||
cookie.max_age = opts.expires
|
||||
local now = os.time()
|
||||
cookie.expires = now + opts.expires
|
||||
elseif opts.expires < 0 then
|
||||
cookie.expires = 1
|
||||
cookie.max_age = 0
|
||||
-- Session cookie (default)
|
||||
else
|
||||
-- opts.expires == 0: Session cookie
|
||||
-- Do nothing (omitting both expires and max-age creates a session cookie)
|
||||
-- Expire immediately
|
||||
cookie.expires = 0
|
||||
cookie.max_age = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Security flags
|
||||
cookie.secure = (opts.secure ~= false)
|
||||
cookie.http_only = (opts.http_only ~= false)
|
||||
-- Set flags (http_only defaults to true)
|
||||
cookie.secure = opts.secure or false
|
||||
cookie.http_only = (opts.http_only ~= false) -- Default to true unless explicitly set to false
|
||||
|
||||
-- Store in cookies table
|
||||
local n = #resp.cookies + 1
|
||||
|
|
Loading…
Reference in New Issue
Block a user