Skip to content

Commit 6c184d5

Browse files
authored
Merge pull request #223 from MDA2AV/reso442
update results - upload
2 parents 75719ae + 9f5eaa5 commit 6c184d5

68 files changed

Lines changed: 57299 additions & 66960 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

frameworks/actix/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ num_cpus = "1"
1414
rusqlite = { version = "0.31", features = ["bundled"] }
1515
tokio-postgres = { version = "0.7", features = ["with-serde_json-1"] }
1616
deadpool-postgres = { version = "0.14", features = ["rt_tokio_1"] }
17+
futures-util = "0.3"
1718

1819
[profile.release]
1920
opt-level = 3

frameworks/actix/src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use actix_web::http::header::{ContentType, HeaderValue, SERVER};
22
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
3+
use futures_util::StreamExt;
34
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
45
use rusqlite::Connection;
56
use rustls::ServerConfig;
@@ -161,11 +162,17 @@ async fn baseline11_get(req: HttpRequest) -> HttpResponse {
161162
.body(sum.to_string())
162163
}
163164

164-
async fn upload(body: web::Bytes) -> HttpResponse {
165+
async fn upload(mut payload: web::Payload) -> HttpResponse {
166+
let mut size: usize = 0;
167+
while let Some(chunk) = payload.next().await {
168+
if let Ok(data) = chunk {
169+
size += data.len();
170+
}
171+
}
165172
HttpResponse::Ok()
166173
.insert_header((SERVER, SERVER_HDR.clone()))
167174
.content_type(ContentType::plaintext())
168-
.body(body.len().to_string())
175+
.body(size.to_string())
169176
}
170177

171178
async fn baseline11_post(req: HttpRequest, body: web::Bytes) -> HttpResponse {

frameworks/aspnet-minimal/Handlers.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ public static async ValueTask<int> SumBody(int a, int b, HttpRequest req)
1515

1616
public static async Task<IResult> Upload(HttpRequest req)
1717
{
18-
using var ms = new MemoryStream();
19-
await req.Body.CopyToAsync(ms);
20-
return Results.Text(ms.Length.ToString());
18+
long size = 0;
19+
var buffer = new byte[65536];
20+
int read;
21+
while ((read = await req.Body.ReadAsync(buffer)) > 0)
22+
{
23+
size += read;
24+
}
25+
return Results.Text(size.ToString());
2126
}
2227

2328
public static IResult Json()

frameworks/bun/server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,13 @@ async function handleRequest(req: Request): Promise<Response> {
194194
}
195195

196196
if (path === "/upload" && req.method === "POST") {
197-
return req.arrayBuffer().then((ab) => {
198-
return new Response(String(ab.byteLength), {
199-
headers: { "content-type": "text/plain" },
200-
});
201-
});
197+
let size = 0;
198+
if (req.body) {
199+
for await (const chunk of req.body) {
200+
size += chunk.byteLength;
201+
}
202+
}
203+
return new Response(String(size), { headers: { "content-type": "text/plain" } });
202204
}
203205

204206
// /baseline11 — GET or POST

frameworks/caddy/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
234234

235235
case "/upload":
236236
if r.Method == "POST" && r.Body != nil {
237-
body, _ := io.ReadAll(r.Body)
237+
n, _ := io.Copy(io.Discard, r.Body)
238238
w.Header().Set("Content-Type", "text/plain")
239239
w.Header().Set("Server", "caddy")
240-
fmt.Fprintf(w, "%d", len(body))
240+
fmt.Fprintf(w, "%d", n)
241241
} else {
242242
http.Error(w, "POST required", 405)
243243
}

frameworks/chi/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77
"database/sql"
88
"encoding/json"
9-
"fmt"
109
"io"
1110
"math"
1211
"mime"
@@ -239,9 +238,9 @@ func main() {
239238
})
240239

241240
r.Post("/upload", func(w http.ResponseWriter, r *http.Request) {
242-
body, _ := io.ReadAll(r.Body)
241+
size, _ := io.Copy(io.Discard, r.Body)
243242
w.Header().Set("Server", "chi")
244-
w.Write([]byte(fmt.Sprintf("%d", len(body))))
243+
w.Write([]byte(strconv.FormatInt(size, 10)))
245244
})
246245

247246
r.Get("/db", func(w http.ResponseWriter, r *http.Request) {

frameworks/deno/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,13 @@ export default {
158158
}
159159

160160
if (path === "/upload" && req.method === "POST") {
161-
const buf = new Uint8Array(await req.arrayBuffer());
162-
return new Response(String(buf.byteLength), { headers: PLAIN });
161+
let size = 0;
162+
if (req.body) {
163+
for await (const chunk of req.body) {
164+
size += chunk.byteLength;
165+
}
166+
}
167+
return new Response(String(size), { headers: PLAIN });
163168
}
164169

165170
// /baseline11

frameworks/echo/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77
"database/sql"
88
"encoding/json"
9-
"fmt"
109
"io"
1110
"math"
1211
"mime"
@@ -241,9 +240,9 @@ func main() {
241240
})
242241

243242
e.POST("/upload", func(c echo.Context) error {
244-
body, _ := io.ReadAll(c.Request().Body)
243+
size, _ := io.Copy(io.Discard, c.Request().Body)
245244
c.Response().Header().Set("Server", "echo")
246-
return c.String(http.StatusOK, fmt.Sprintf("%d", len(body)))
245+
return c.String(http.StatusOK, strconv.FormatInt(size, 10))
247246
})
248247

249248
e.GET("/db", func(c echo.Context) error {

frameworks/elysia/server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,13 @@ function addRoutes(app: Elysia) {
187187
})
188188

189189
.post("/upload", async ({ request }) => {
190-
const ab = await request.arrayBuffer();
191-
return new Response(String(ab.byteLength), {
190+
let size = 0;
191+
if (request.body) {
192+
for await (const chunk of request.body) {
193+
size += chunk.byteLength;
194+
}
195+
}
196+
return new Response(String(size), {
192197
headers: { "content-type": "text/plain" },
193198
});
194199
})

frameworks/express/server.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,16 @@ function startWorker() {
8888
const express = require('express');
8989
const app = express();
9090

91-
// Raw body parsing
91+
// --- /upload --- (defined before body-parsing middleware so the stream is not buffered)
92+
app.post('/upload', (req, res) => {
93+
let size = 0;
94+
req.on('data', chunk => { size += chunk.length; });
95+
req.on('end', () => {
96+
res.set('server', SERVER_NAME).set('Content-Type', 'text/plain').send(String(size));
97+
});
98+
});
99+
100+
// Raw body parsing (applied to all routes except /upload which is already defined above)
92101
app.use(express.raw({ type: 'application/octet-stream', limit: '50mb' }));
93102
app.use(express.text({ type: 'text/plain', limit: '50mb' }));
94103
app.use(express.raw({ type: '*/*', limit: '50mb' }));
@@ -204,12 +213,6 @@ function startWorker() {
204213
}
205214
});
206215

207-
// --- /upload ---
208-
app.post('/upload', (req, res) => {
209-
const body = Buffer.isBuffer(req.body) ? req.body : Buffer.from(req.body || '');
210-
res.set('server', SERVER_NAME).type('text/plain').send(String(body.length));
211-
});
212-
213216
// Start HTTP/1.1 server
214217
const server = http.createServer(app);
215218
server.listen(8080, '0.0.0.0', () => {

0 commit comments

Comments
 (0)