"""同一连接池逐帧下载并验证每个字节;独立对端不使用 Dever 运行库。""" from http_pool_reference import source as pool_source def source(language): base = pool_source(language) if language == "dever": helpers = '''checkedChunk(offset: Int, bytes: Bytes, expected: Bytes) (next: Int) { next = offset + length(bytes) check bytes == dever.bytes.slice(expected, offset, next) else Invalid } rule piece(offset: Int, event: dever.io.ReadEvent, expected: Bytes) (next: Int) | offset | event | expected | next | | - | Chunk(bytes) | - | checkedChunk(offset, bytes, expected) | | - | Failed(message) | - | fail(dever.io.ReadEvent.Failed(message)) | loop download(body: AsyncStream, expected: Bytes) (size: Int) | each event in body | result | | size | fold(offset = 0, piece(offset, event, expected)) | ''' return helpers + base.replace("dever.http.request(client, request)", "dever.http.open(client, request)").replace( "| - | check | response.body == body else Invalid |", "| size | loop | download(response.body, body) |\n| - | check | size == length(body) else Invalid |") if language == "rust": base = base.replace("tokio::time::timeout(Duration::from_secs(5), async {", "{").replace( "let response = client.request(request).await.unwrap();", "let response = tokio::time::timeout(Duration::from_secs(5), client.request(request)).await.unwrap().unwrap();").replace( " }).await.unwrap();", " }") return base.replace("BodyExt, Full, Limited", "BodyExt, Full").replace( "let response = Limited::new(response.into_body(), 1048576).collect().await.unwrap().to_bytes();\n assert_eq!(response, body);", '''let mut incoming = response.into_body(); let mut offset = 0; while let Some(frame) = tokio::time::timeout(Duration::from_secs(60), incoming.frame()).await.unwrap() { if let Ok(chunk) = frame.unwrap().into_data() { let end = offset + chunk.len(); assert_eq!(chunk, body.slice(offset..end)); offset = end; } } assert_eq!(offset, body.len());''') if language == "go": base = base.replace(" total := int64(0)", " buffer := make([]byte, 65536)\n total := int64(0)") return base.replace( '''received, err := io.ReadAll(io.LimitReader(response.Body, 1048577)); if err != nil { panic(err) } if err := response.Body.Close(); err != nil { panic(err) } if response.StatusCode != 200 || len(received) > 1048576 || !bytes.Equal(received, body) { panic("response mismatch") }''', '''offset := 0 for { count, err := response.Body.Read(buffer) if count > 0 { end := offset + count if end > len(body) || !bytes.Equal(buffer[:count], body[offset:end]) { panic("response mismatch") } offset = end } if err == io.EOF { break } if err != nil { panic(err) } } if err := response.Body.Close(); err != nil { panic(err) } if response.StatusCode != 200 || offset != len(body) { panic("response mismatch") }''') raise ValueError(language)