Skip to content

Commit

Permalink
[BREAKING] ae.sys.net: Switch to ubyte[]
Browse files Browse the repository at this point in the history
Rationale:

- See 70d3c53.

Changes required:

- Implementations of the Network class should update their .getFile and
  .post overrides to use ubyte[].

- If necessary, callers should switch to using ubyte[].
  • Loading branch information
CyberShadow committed Jan 9, 2024
1 parent 3d23fe5 commit 1b42cfc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions sys/net/ae.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ class AENetwork : Network
});
} ///

override void[] getFile(string url)
override ubyte[] getFile(string url)
{
return getData(url).toGC();
} ///

override void[] post(string url, const(void)[] data)
override ubyte[] post(string url, const(ubyte)[] data)
{
Data result;
bool got;

httpPost(url, DataVec(Data(cast(const(ubyte)[])data)), null,
httpPost(url, DataVec(Data(data)), null,
(Data data) { result = data; got = true; },
(string error) { throw new Exception(error); }
);
Expand Down
16 changes: 9 additions & 7 deletions sys/net/cachedcurl.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import ae.sys.dataio;
import ae.sys.dataset;
import ae.sys.file;
import ae.sys.net;
import ae.utils.array;
import ae.utils.digest;
import ae.utils.json;
import ae.utils.time;
Expand Down Expand Up @@ -71,7 +72,7 @@ class CachedCurlNetwork : Network
{
string url; ///
HTTP.Method method = HTTP.Method.get; ///
const(void)[] data; ///
const(ubyte)[] data; ///
const(string[2])[] headers; ///

/// Maximum number of redirects to follow.
Expand Down Expand Up @@ -114,10 +115,11 @@ class CachedCurlNetwork : Network
};
if (request.data)
{
const(void)[] data = request.data;
const(ubyte)[] data = request.data;
http.addRequestHeader("Content-Length", data.length.text);
http.onSend = (void[] buf)
http.onSend = (void[] voidBuf)
{
auto buf = cast(ubyte[])voidBuf;
size_t len = min(buf.length, data.length);
buf[0..len] = data[0..len];
data = data[len..$];
Expand Down Expand Up @@ -179,7 +181,7 @@ class CachedCurlNetwork : Network
/// Perform a raw request and return information about the resulting cached response.
Response cachedReq(ref const Request request)
{
auto hash = getDigestString!MD5(request.url ~ cast(char)request.method ~ request.data);
auto hash = getDigestString!MD5(request.url.asBytes ~ cast(char)request.method ~ request.data);
auto path = buildPath(cacheDir, hash[0..2], hash);
ensurePathExists(path);
auto metadataPath = path ~ ".metadata";
Expand All @@ -190,7 +192,7 @@ class CachedCurlNetwork : Network
}

/// ditto
Response cachedReq(string url, HTTP.Method method, const(void)[] data = null)
Response cachedReq(string url, HTTP.Method method, const(ubyte)[] data = null)
{
auto req = Request(url, method, data);
return cachedReq(req);
Expand All @@ -206,7 +208,7 @@ class CachedCurlNetwork : Network
std.file.copy(downloadFile(url), target);
} ///

override void[] getFile(string url)
override ubyte[] getFile(string url)
{
return cachedReq(url, HTTP.Method.get).responseData;
} ///
Expand All @@ -228,7 +230,7 @@ class CachedCurlNetwork : Network
[$-1]);
} ///

override void[] post(string url, const(void)[] data)
override ubyte[] post(string url, const(ubyte)[] data)
{
return cachedReq(url, HTTP.Method.post, data).responseData;
} ///
Expand Down
4 changes: 2 additions & 2 deletions sys/net/curl.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class CurlNetwork : Network
std.file.write(target, getFile(url));
} ///

override void[] getFile(string url)
override ubyte[] getFile(string url)
{
return get!(AutoProtocol, ubyte)(url);
} ///

override void[] post(string url, const(void)[] data)
override ubyte[] post(string url, const(ubyte)[] data)
{
return .post!ubyte(url, data);
} ///
Expand Down
6 changes: 3 additions & 3 deletions sys/net/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ class Network
notImplemented();
}

// TODO: use ubyte[] instead of void[]
// TODO: use Data instead of ubyte[]?

/// Get resource located at the indicated URL.
void[] getFile(string url)
ubyte[] getFile(string url)
{
notImplemented();
assert(false);
}

/// Post data to the specified URL.
// TODO: Content-Type?
void[] post(string url, const(void)[] data)
ubyte[] post(string url, const(ubyte)[] data)
{
notImplemented();
assert(false);
Expand Down
4 changes: 2 additions & 2 deletions sys/net/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import std.process : environment;
import ae.net.http.common;
import ae.sys.data : Data;
import ae.sys.dataset : DataVec, joinToGC;
import ae.utils.array : asBytes;
import ae.utils.array : asBytes, as;

static import ae.sys.net.ae;
static import ae.sys.net.curl;
Expand Down Expand Up @@ -75,7 +75,7 @@ void test(string moduleName, string className)()

debug std.stdio.stderr.writeln(" - post");
{
auto result = cast(string)net.post(testBaseURL ~ "testUrl4", "Hello world\n");
auto result = net.post(testBaseURL ~ "testUrl4", "Hello world\n".asBytes).as!string;
assert(result == "Hello world\n", result);
}

Expand Down
6 changes: 3 additions & 3 deletions sys/net/wininet.d
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected:
return HNet(hReq);
}

final static void sendRequest(ref HNet hReq, string headers = null, const(void)[] optionalData = null)
final static void sendRequest(ref HNet hReq, string headers = null, const(ubyte)[] optionalData = null)
{
HttpSendRequestA(hReq, headers.ptr, headers.length.to!DWORD, cast(void*)optionalData.ptr, optionalData.length.to!DWORD)
.wenforce("HttpSendRequest");
Expand Down Expand Up @@ -195,7 +195,7 @@ public:
hReq.I!doDownload(&f.rawWrite!ubyte);
} ///

override void[] getFile(string url)
override ubyte[] getFile(string url)
{
auto result = appender!(ubyte[]);
auto hNet = open();
Expand All @@ -205,7 +205,7 @@ public:
return result.data;
} ///

override void[] post(string url, const(void)[] data)
override ubyte[] post(string url, const(ubyte)[] data)
{
auto request = new HttpRequest(url);

Expand Down

0 comments on commit 1b42cfc

Please sign in to comment.