Skip to content

Commit

Permalink
don't init req.cookies before checking the header (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday authored Aug 17, 2023
1 parent d942eb7 commit 18c624c
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const kReplySetCookies = Symbol('fastify.reply.setCookies')
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')

function fastifyCookieSetCookie (reply, name, value, options) {
parseCookies(reply.context.server, reply.request, reply)
parseCookies(reply.server, reply.request, reply)

const opts = Object.assign({}, options)

Expand Down Expand Up @@ -51,11 +51,9 @@ function fastifyCookieClearCookie (reply, name, options) {
function parseCookies (fastify, request, reply) {
if (reply[kReplySetCookies]) return

request.cookies = {} // New container per request. Issue #53
const cookieHeader = request.raw.headers.cookie
if (cookieHeader) {
request.cookies = fastify.parseCookie(cookieHeader)
}

request.cookies = cookieHeader ? fastify.parseCookie(cookieHeader) : {} // New container per request. Issue #53
reply[kReplySetCookies] = new Map()
}

Expand Down Expand Up @@ -116,18 +114,6 @@ function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
done()
}

function getHook (hook = 'onRequest') {
const hooks = {
onRequest: 'onRequest',
preParsing: 'preParsing',
preValidation: 'preValidation',
preHandler: 'preHandler',
[false]: false
}

return hooks[hook]
}

function plugin (fastify, options, next) {
const secret = options.secret
const hook = getHook(options.hook)
Expand Down Expand Up @@ -190,6 +176,18 @@ function plugin (fastify, options, next) {
}
}

function getHook (hook = 'onRequest') {
const hooks = {
onRequest: 'onRequest',
preParsing: 'preParsing',
preValidation: 'preValidation',
preHandler: 'preHandler',
[false]: false
}

return hooks[hook]
}

function isConnectionSecure (request) {
return (
request.raw.socket?.encrypted === true ||
Expand Down

0 comments on commit 18c624c

Please sign in to comment.