Skip to content

Commit

Permalink
Merge pull request syumai#37 from syumai/fix-space-decoding-in-form
Browse files Browse the repository at this point in the history
Fix space decoding in form params
  • Loading branch information
syumai authored May 18, 2019
2 parents 8f7c357 + 96dffc2 commit 9248a35
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
5 changes: 1 addition & 4 deletions handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Response } from './response.ts';
import { Params } from './params.ts';

export enum Method {
GET = 'GET',
Expand All @@ -11,10 +12,6 @@ export enum Method {
UNLINK = 'UNLINK',
}

export interface Params {
[key: string]: any;
}

export type Context = {
readonly path: string;
readonly method: Method;
Expand Down
21 changes: 8 additions & 13 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
import { decode } from 'https://deno.land/std/strings/strings.ts';
import { Response, processResponse } from './response.ts';
import { ErrorCode, getErrorMessage } from './errors.ts';
import { Method, Params, Handler, HandlerConfig } from './handler.ts';
import { Method, Handler, HandlerConfig } from './handler.ts';
import { Params, parseURLSearchParams } from './params.ts';
import { defaultPort } from './constants.ts';
import { detectedContentType } from './mime.ts';
export { contentType, detectedContentType } from './mime.ts';
Expand Down Expand Up @@ -95,20 +96,14 @@ export class App {
const params: Params = {};
if (method === Method.GET) {
if (search) {
for (const [key, value] of new URLSearchParams(search).entries()) {
params[key] = value;
}
Object.assign(params, parseURLSearchParams(search));
}
} else {
const decodedBody = decode(await readAll(req.body)); // FIXME: this line is broken
const decodedBody = decode(await readAll(req.body)); // FIXME: this line is should be refactored using Deno.Reader
const contentType = req.headers.get('content-type');
switch (contentType) {
case 'application/x-www-form-urlencoded':
for (const [key, value] of new URLSearchParams(
decodedBody
).entries()) {
params[key] = value;
}
Object.assign(params, parseURLSearchParams(decodedBody));
break;
case 'application/json':
let obj: Object;
Expand All @@ -117,9 +112,7 @@ export class App {
} catch (e) {
throw ErrorCode.BadRequest;
}
for (const [key, value] of Object.entries(obj)) {
params[key] = value;
}
Object.assign(params, obj);
break;
}
}
Expand Down Expand Up @@ -159,6 +152,8 @@ export class App {
let status = ErrorCode.InternalServerError;
if (typeof err === 'number') {
status = err;
} else {
console.error(err);
}
r = [status, getErrorMessage(status)];
}
Expand Down
12 changes: 12 additions & 0 deletions params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface Params {
[key: string]: any;
}

export function parseURLSearchParams(paramsStr: string): Params {
const params: Params = {};
const spaceReplacedStr = paramsStr.replace(/\+/g, ' '); // replace all + to spaces
for (const [key, value] of new URLSearchParams(spaceReplacedStr).entries()) {
params[key] = value;
}
return params;
}
2 changes: 1 addition & 1 deletion serve_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, runTests } from 'https://deno.land/std/testing/mod.ts';
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
import { App, get, post } from './mod.ts';
import { HandlerConfig, Method, Params } from './handler.ts';
import { HandlerConfig, Method } from './handler.ts';
const { exit } = Deno;

const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
Expand Down

0 comments on commit 9248a35

Please sign in to comment.