Skip to content

Commit

Permalink
fix: decode unicode for response body
Browse files Browse the repository at this point in the history
fixed #142
  • Loading branch information
buqiyuan committed Jan 4, 2023
1 parent 6692e26 commit 2d6f125
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { editor, IDisposable } from 'monaco-editor';
import type { JoinedEditorOptions } from 'ng-zorro-antd/code-editor';

import { ThemeService } from '../../../core/services/theme.service';
import { b64DecodeUnicode, debounce, whatTextType } from '../../../utils/index.utils';
import { debounce, whatTextType } from '../../../utils/index.utils';
import { getDefaultCompletions } from './defaultCompletions';

type EventType = 'format' | 'copy' | 'search' | 'replace' | 'type' | 'download' | 'newTab';
Expand Down Expand Up @@ -35,12 +35,6 @@ const eventHash = new Map()
export class EoMonacoEditorComponent implements AfterViewInit, OnInit, OnChanges, OnDestroy {
@Input() eventList: EventType[] = [];
@Input() hiddenList: string[] = [];
@Input() set isBase64(val) {
this.$$isBase64 = val;
if (val) {
this.setCode(b64DecodeUnicode(this.$$code));
}
}
@Input() set code(val) {
this.setCode(val);
}
Expand All @@ -54,7 +48,6 @@ export class EoMonacoEditorComponent implements AfterViewInit, OnInit, OnChanges
@Input() completions = [];
@Output() readonly codeChange = new EventEmitter<string>();
$$code = '';
$$isBase64 = false;
isFirstFormat = true;
codeEdtor: editor.IStandaloneCodeEditor;
completionItemProvider: IDisposable;
Expand Down Expand Up @@ -187,9 +180,7 @@ export class EoMonacoEditorComponent implements AfterViewInit, OnInit, OnChanges

let code = val;
try {
if (this.$$isBase64) {
code = b64DecodeUnicode(val);
} else if (typeof val === 'object') {
if (typeof val === 'object') {
code = JSON.stringify(val);
} else {
// code = JSON.stringify(typeof val === 'string' ? JSON.parse(val) : val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@
class="mt-[20px] border-all"
[autoFormat]="true"
[autoType]="true"
[code]="model.body"
[code]="responseBody"
[config]="{ readOnly: true }"
[isBase64]="['longText', 'stream'].includes(model.responseType)"
[eventList]="['type', 'format', 'copy', 'search']"
></eo-monaco-editor>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, Input, OnInit, OnChanges, ViewChild } from '@angular/core';
import { SafeUrl } from '@angular/platform-browser';
import { EoMonacoEditorComponent } from 'eo/workbench/browser/src/app/modules/eo-ui/monaco-editor/monaco-editor.component';
import { b64DecodeUnicode, getBlobUrl } from 'eo/workbench/browser/src/app/utils/index.utils';
import { b64DecodeUnicode, decodeUnicode, getBlobUrl } from 'eo/workbench/browser/src/app/utils/index.utils';
import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';

import { ApiTestUtilService } from '../../../../../../../modules/api-shared/api-test-util.service';
Expand All @@ -19,6 +19,7 @@ export class ApiTestResultResponseComponent implements OnChanges {
codeStatus: { status: string; cap: number; class: string };
size: string;
blobUrl = '';
responseBody = '';
get responseIsImg() {
return this.model.contentType?.startsWith('image');
}
Expand All @@ -28,6 +29,7 @@ export class ApiTestResultResponseComponent implements OnChanges {
ngOnChanges(changes) {
if (changes.model && this.model) {
this.codeStatus = this.apiTest.getHTTPStatus(this.model?.statusCode);
this.responseBody = this.decodeBody(changes.model.currentValue.body || '');
if (!this.responseIsImg) {
this.eoEditor?.formatCode();
} else if (this.responseIsImg) {
Expand All @@ -36,6 +38,14 @@ export class ApiTestResultResponseComponent implements OnChanges {
}
}

decodeBody(body: string) {
if (['longText', 'stream'].includes(this.model.responseType)) {
return decodeUnicode(b64DecodeUnicode(body));
} else {
return decodeUnicode(body);
}
}

contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent): void {
this.nzContextMenuService.create($event, menu);
}
Expand Down
6 changes: 5 additions & 1 deletion src/workbench/browser/src/app/utils/index.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export const compareVersion = (v1, v2) => {
return _r === 0 && v1 !== v2 ? compareVersion(_v1.splice(1).join('.'), _v2.splice(1).join('.')) : _r;
};

// more see https://developer.mozilla.org/zh-CN/docs/Glossary/Base64#solution_4_%E2%80%93_escaping_the_string_before_encoding_it
// more see https://developer.mozilla.org/zh-CN/docs/Glossary/Base64#solution_4_–_escaping_the_string_before_encoding_it
export const b64DecodeUnicode = (str: string) => {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(
Expand All @@ -300,3 +300,7 @@ export const b64DecodeUnicode = (str: string) => {
.join('')
);
};

export const decodeUnicode = (str: string) => {
return unescape(str.replace(/\\u/gi, '%u'));
};

0 comments on commit 2d6f125

Please sign in to comment.