Skip to content

Commit

Permalink
Add basic support for receiving binary data in XMLHTTPRequest (also a…
Browse files Browse the repository at this point in the history
…ffected XHR powered fetch)
  • Loading branch information
Riku Ayanokoji committed Jun 6, 2016
1 parent 42a5568 commit a40a0a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Libraries/Network/RCTNetworking.m
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ - (void)sendData:(NSData *)data forTask:(RCTNetworkTask *)task
return;
}

/*
// Get text encoding
NSURLResponse *response = task.response;
NSStringEncoding encoding = NSUTF8StringEncoding;
Expand Down Expand Up @@ -350,8 +351,9 @@ - (void)sendData:(NSData *)data forTask:(RCTNetworkTask *)task
RCTLogWarn(@"Received data was not a string, or was not a recognised encoding.");
return;
}
}
}*/

NSString *responseText = [data base64EncodedStringWithOptions:0];
NSArray<id> *responseJSON = @[task.requestID, responseText ?: @""];
[self sendEventWithName:@"didReceiveNetworkData" body:responseJSON];
}
Expand Down
1 change: 1 addition & 0 deletions Libraries/Network/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
}

__didReceiveData(requestId: number, responseText: string): void {
responseText = atob(responseText);
if (requestId === this._requestId) {
if (!this._responseText) {
this._responseText = responseText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

package com.facebook.react.modules.network;

import android.util.Base64;

import javax.annotation.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -284,7 +286,7 @@ public void onResponse(Call call, Response response) throws IOException {
readWithProgress(executorToken, requestId, responseBody);
onRequestSuccess(executorToken, requestId);
} else {
onDataReceived(executorToken, requestId, responseBody.string());
onDataReceived(executorToken, requestId, responseBody.bytes());
onRequestSuccess(executorToken, requestId);
}
} catch (IOException e) {
Expand All @@ -298,12 +300,12 @@ private void readWithProgress(
ExecutorToken executorToken,
int requestId,
ResponseBody responseBody) throws IOException {
Reader reader = responseBody.charStream();
Reader reader = responseBody.byteStream();
try {
char[] buffer = new char[MAX_CHUNK_SIZE_BETWEEN_FLUSHES];
byte[] buffer = new byte[MAX_CHUNK_SIZE_BETWEEN_FLUSHES];
int read;
while ((read = reader.read(buffer)) != -1) {
onDataReceived(executorToken, requestId, new String(buffer, 0, read));
onDataReceived(executorToken, requestId, buffer);
}
} finally {
reader.close();
Expand All @@ -322,10 +324,10 @@ private void onDataSend(ExecutorToken ExecutorToken, int requestId, long progres
getEventEmitter(ExecutorToken).emit("didSendNetworkData", args);
}

private void onDataReceived(ExecutorToken ExecutorToken, int requestId, String data) {
private void onDataReceived(ExecutorToken ExecutorToken, int requestId, byte[] data) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushString(data);
args.pushString(Base64.encodeToString(data, 0));

getEventEmitter(ExecutorToken).emit("didReceiveNetworkData", args);
}
Expand Down

0 comments on commit a40a0a1

Please sign in to comment.