Skip to content

Commit e55bcde

Browse files
committed
鸿蒙细节调整
1 parent 5513ffd commit e55bcde

File tree

2 files changed

+47
-25
lines changed
  • packages

2 files changed

+47
-25
lines changed

packages/browser/src/http/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import common, { HttpRequestError, HttpResponse, MockProgress, UploadError, isHttpFormData, isSuccessResult } from '@internal/common'
1+
import common, { HttpResponse, MockProgress, UploadError, isHttpFormData } from '@internal/common'
22

3-
import { UploadFile, isUploadBlob, isUploadFile } from '../file'
3+
import { isUploadBlob, isUploadFile } from '../file'
44

55
interface RequestOptions extends common.HttpClientOptions {
66
method: common.HttpMethod

packages/harmony/library/src/main/ets/components/http/index.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import http from '@ohos.net.http'
2-
import uploadFile from '@ohos.request'
2+
import requestApi from '@ohos.request'
33
import ohCommon from '@ohos.app.ability.common'
44

55
import * as common from '../@internal'
@@ -9,6 +9,28 @@ interface RequestOptions extends common.HttpClientOptions {
99
method: common.HttpMethod
1010
}
1111

12+
function parseHeader(header: string): {
13+
statusCode: number
14+
header: common.HttpHeader
15+
} {
16+
17+
const newHeader: common.HttpHeader = {}
18+
const delimiter = '\r\n'
19+
const [first, ...lines] = header.split(delimiter).filter(v => v !== '')
20+
for (let index = 0; index < lines.length; index++) {
21+
const line = lines[index]
22+
const [key, value] = line.split(':')
23+
newHeader[key] = value.trim()
24+
}
25+
26+
return {
27+
// HTTP/1.1 200 OK
28+
statusCode: parseInt(first.split(' ')[1], 10),
29+
header: newHeader
30+
}
31+
}
32+
33+
1234
function transformRequestMethod(method: common.HttpMethod): http.RequestMethod {
1335
if (method === 'PUT') return http.RequestMethod.PUT
1436
if (method === 'GET') return http.RequestMethod.GET
@@ -24,20 +46,14 @@ function shouldUseUploadFile(option: RequestOptions): boolean {
2446
return files.length > 0
2547
}
2648

27-
function normalizeFormHeader(header: common.HttpHeader = {}): common.HttpHeader {
28-
const boundary = `----WebKitFormBoundary${common.generateRandomString(12)}`
29-
header['content-type'] = `multipart/form-data; boundary=${boundary}`
30-
return header
31-
}
32-
3349
export class HttpClient implements HttpClient {
3450
constructor(private context: ohCommon.BaseContext) {}
3551

3652
private async request(url: string, options: RequestOptions): Promise<common.Result<common.HttpResponse>> {
37-
// 使用 uploadFile 接口发送请求
53+
// 使用 requestApi 接口发送请求
3854
if (shouldUseUploadFile(options)) {
39-
const files: uploadFile.File[] = []
40-
const formData: uploadFile.RequestData[] = []
55+
const files: requestApi.File[] = []
56+
const formData: requestApi.RequestData[] = []
4157
const bodyEntries = (options.body as common.HttpFormData).entries()
4258

4359
for (const [key, value] of bodyEntries) {
@@ -62,17 +78,16 @@ export class HttpClient implements HttpClient {
6278
}
6379

6480
return new Promise(resolve => {
65-
const uploadFileOptions: uploadFile.UploadConfig = {
66-
url,
81+
const uploadFileOptions: requestApi.UploadConfig = {
6782
files,
6883
data: formData,
69-
header: normalizeFormHeader(options.headers),
70-
method: transformRequestMethod(options.method)
84+
url: url.toLowerCase(),
85+
method: options.method,
86+
header: options.headers
7187
}
7288

7389
try {
74-
// FIXME: 这个 api 现在有问题,用不了一点
75-
uploadFile.uploadFile(this.context, common.removeUndefinedKeys(uploadFileOptions))
90+
requestApi.uploadFile(this.context, common.removeUndefinedKeys(uploadFileOptions))
7691
.then(task => {
7792
if (options.abort) {
7893
options.abort.onAbort(() => task.delete())
@@ -85,21 +100,28 @@ export class HttpClient implements HttpClient {
85100
))
86101
}
87102

88-
task.on('complete', states => {
89-
const firstState = states[0]
90-
mockProgress.end()
103+
let responseCode: number = 0
104+
let responseHeader: common.HttpHeader = {}
105+
task.on('headerReceive', header => {
106+
if (typeof header === 'string') {
107+
const data = parseHeader(header)
108+
responseCode = data.statusCode
109+
header = data.header
110+
}
111+
})
112+
113+
task.on('complete', () => {
91114
return resolve({
92115
result: {
93-
data: firstState.message,
94-
code: firstState.responseCode,
95-
reqId: 'UploadFile api cannot get this value'
116+
data: '', // TODO: 暂时不支持读取 body,next 版本将会支持
117+
code: responseCode,
118+
reqId: responseHeader['X-Reqid']
96119
}
97120
})
98121
})
99122

100123
task.on('fail', states => {
101124
const firstState = states[0]
102-
mockProgress.end()
103125
return resolve({
104126
result: {
105127
data: firstState.message,

0 commit comments

Comments
 (0)