Skip to content

Commit

Permalink
feat: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Mar 26, 2024
1 parent 1327c02 commit 228a363
Showing 1 changed file with 52 additions and 49 deletions.
101 changes: 52 additions & 49 deletions src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
import { Context } from 'koa';

/**
* @function decodeURI
* @description Decode URI component.
* @param URI The URI to decode.
* @function isETag
* @description Check if etag is valid.
* @param value The value to check.
*/
export function decodeURI(URI: string): string | -1 {
try {
return decodeURIComponent(URI);
} catch {
return -1;
}
function isETag(value: string): boolean {
return /^(?:W\/)?"[\s\S]+"$/.test(value);
}

/**
* @function parseTokens
* @description Parse HTTP tokens.
* @param value The tokens value string.
*/
export function parseTokens(value: string): string[] {
function parseTokens(value: string): string[] {
let end = 0;
let start = 0;
let tokens: string[] = [];
Expand Down Expand Up @@ -53,15 +49,6 @@ export function parseTokens(value: string): string[] {
return tokens;
}

/**
* @function isETag
* @description Check if etag is valid.
* @param value The value to check.
*/
function isETag(value: string): boolean {
return /^(?:W\/)?"[\s\S]+"$/.test(value);
}

/**
* @function isETagFresh
* @description Check if etag is fresh.
Expand All @@ -74,6 +61,19 @@ function isETagFresh(match: string, etag: string): boolean {
});
}

/**
* @function decodeURI
* @description Decode URI component.
* @param URI The URI to decode.
*/
export function decodeURI(URI: string): string | -1 {
try {
return decodeURIComponent(URI);
} catch {
return -1;
}
}

/**
* @function isConditionalGET
* @description Check if request is conditional GET.
Expand All @@ -86,10 +86,42 @@ export function isConditionalGET(context: Context): boolean {
request.get('If-Match') ||
request.get('If-None-Match') ||
request.get('If-Modified-Since') ||
request.get('if-Unmodified-Since')
request.get('If-Unmodified-Since')
);
}

/**
* @function isPreconditionFailure
* @description Check if request precondition failure.
* @param context Koa context.
*/
export function isPreconditionFailure({ request, response }: Context): boolean {
// If-Match.
const match = request.get('If-Match');

// Check if request match.
if (match) {
// Etag.
const etag = response.get('ETag');

return !etag || match === '*' || !isETagFresh(match, etag);
}

// If-Unmodified-Since.
const unmodifiedSince = Date.parse(request.get('If-Unmodified-Since'));

// Check if request unmodified.
if (!Number.isNaN(unmodifiedSince)) {
// Last-Modified.
const lastModified = Date.parse(response.get('Last-Modified'));

return Number.isNaN(lastModified) || lastModified > unmodifiedSince;
}

// Check precondition passed.
return false;
}

/**
* @function isRangeFresh
* @description Check if request range fresh.
Expand All @@ -116,32 +148,3 @@ export function isRangeFresh(context: Context): boolean {

return Date.parse(lastModified) <= Date.parse(ifRange);
}

/**
* @function isPreconditionFailure
* @description Check if request precondition failure.
* @param context Koa context.
*/
export function isPreconditionFailure(context: Context): boolean {
const { request, response } = context;

// If-Match.
const match = request.get('If-Match');

if (match) {
const etag = response.get('ETag');

return !etag || (match !== '*' && !isETagFresh(match, etag));
}

// If-Unmodified-Since.
const unmodifiedSince = Date.parse(request.get('If-Unmodified-Since'));

if (!Number.isNaN(unmodifiedSince)) {
const lastModified = Date.parse(response.get('Last-Modified'));

return Number.isNaN(lastModified) || lastModified > unmodifiedSince;
}

return false;
}

0 comments on commit 228a363

Please sign in to comment.