vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.81k stars 2.16k forks source link

windows: Failed to upload a file via content_type: multipart/form-data #19256

Open Ddiidev opened 1 year ago

Ddiidev commented 1 year ago

Describe the bug

When trying to upload a file with content type: multipart/form-data it breaks. And it works fine with postman/hoppscotch/thunder client etc... you can test here: hoppscotch example

Reproduction Steps

module main

import net.http
import os

const base_url_v1 = 'https://anonfiles.me/api/v1/upload'

fn main() {

    println("Start")

    mut header := http.Header{}
    header.set(.content_type, 'multipart/form-data')
    file := os.read_file("${@VMODROOT}/img.png")!

    println("${file.len} file ok!")
    mut resp := http.post_multipart_form(base_url_v1, http.PostMultipartFormConfig{
        form: {}
        files: {
            'file': [
                http.FileData{
                    filename: 'file'
                    content_type: 'image/png'
                    data: file
                }
            ]
        }
        header: header
    })!

    dump(resp)
}

Expected Behavior

Something like...

{
  "status": true,
  "data": {
    "file": {
      "url": {
        "full": "https://anonfiles.me/EWo/2560x1440-firewatch-digital-art-4k-1548526913",
        "short": "https://anonfiles.me/EWo"
      },
      "metadata": {
        "id": "EWo",
        "name": "2560x1440-firewatch-digital-art-4k_1548526913.jpg",
        "size": {
          "bytes": 227463,
          "readable": "222.13 KB"
        }
      }
    }
  }
}

Current Behavior

V panic: result not set (response does not start with HTTP/, line: ``) v hash: e414b54 C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.1827955018892112456.tmp.c:11311: at _v_panic: Backtrace C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.1827955018892112456.tmp.c:11281: by panic_result_not_set C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.1827955018892112456.tmp.c:32085: by main__main C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.1827955018892112456.tmp.c:32423: by wmain 0050bb78 : by ??? 0050bcdb : by ??? 7ffd90fe1f88 : by ???

Possible Solution

I have no idea

Additional Information/Context

I used TCC, GCC, Clang and MSVC but it didn't work independently.

I need this to make a module for the subbase storage, but this is stopping me. Here is the documentation supabase doc storage

V version

V 0.4.0 0450a93

Environment details (OS name and version, etc.)

V full version: V 0.4.0 bfaa3de.0450a93 OS: windows, Microsoft Windows 11 Pro Insider Preview v25936 64-bit Processor: 8 cpus, 64bit, little endian,

getwd: C:\Users\AndreLuiz vexe: C:\v\v.exe vexe mtime: 2023-09-01 13:41:06

vroot: OK, value: C:\v VMODULES: OK, value: C:\Users\AndreLuiz.vmodules VTMP: OK, value: C:\Users\AndreLuiz\AppData\Local\Temp\v_0

Git version: git version 2.41.0.windows.3 Git vroot status: weekly.2023.35-21-g0450a933-dirty .git/config present: true

CC version: cc (MinGW-W64 x86_64-msvcrt-posix-seh, built by Brecht Sanders) 12.2.0 thirdparty/tcc status: thirdparty-windows-amd64 e90c2620

JalonSolov commented 1 year ago

I just tried your example, and it works fine on Linux:

$ v run http_error.v 
Start
13011 file ok!
[http_error.v:31] resp: http.Response{
    body: '{"status":true,"data":{"file":{"url":{"full":"https://anonfiles.me/qnX/file","short":"https://anonfiles.me/qnX"},"metadata":{"id":"qnX","name":"file","size":{"bytes":13011,"readable":"12.71 KB"}}}}}'
    header: Server: nginx
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Cache-Control: no-cache, private
Date: Sun, 03 Sep 2023 02:35:59 GMT
Access-Control-Allow-Origin: *
Set-Cookie: XSRF-TOKEN=eyJpdiI6ImtRVU9sTkZZM2haS0tlWkVTT2U5VkE9PSIsInZhbHVlIjoiSjBnZGp2UmpZbDFMUUlubWQ4dFFwUUxlNVBxaXVhVFNSblpRaGc3eWQ5ZUI0a3N2ZlJlYTdhUDhWTzFYK1FZOTdsQkhWZHNNbnNSLzBmaitIOWlpQ1M2Q1hhV0liaVQrQjdoenZkTUtEMUVVdDBjcWhRUUJiVlBSNDYzVU1aWXYiLCJtYWMiOiI4YWRlZmUxZGYwNGNkYTkyN2ZlNDcyYWFhNzliNmZlMGM1MWU0YzE5NTdiMTU2YTBmNGE2MjhmNzU4OTYxMGJhIiwidGFnIjoiIn0%3D; expires=Sun, 03-Sep-2023 04:35:59 GMT; Max-Age=7200; path=/; samesite=lax
Set-Cookie: anonfiles_session=eyJpdiI6IisrQXhMVC8xQ2hlUDFXWG12VFdQMVE9PSIsInZhbHVlIjoia1pvQ08rWFA5TTF5Z2dNRUM0emR6bjZaTWtRVWc4cERKMVM1MElzczFSN2FjUWtvZHozMmhoMlF0Wk9YZmZneksyek5Fenl4UXNtZ0JlNXh1Tm8wNWtwRTNIYTBaeVBnbXVwandsL2hxbUQ0aFIzU29CQXBvYXdyUFVpQlJML2oiLCJtYWMiOiJjOTBjOGVkN2JlNmE5NTgxYjgyMmZlNGQwMzE5ODdkMmI4YzdiZGFlYjQwYWYyMDFkZmMyMDYxOGU1YjRjNGQyIiwidGFnIjoiIn0%3D; expires=Sun, 03-Sep-2023 04:35:59 GMT; Max-Age=7200; path=/; httponly; samesite=lax
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

    status_code: 200
    status_msg: 'OK'
    http_version: '1.1'
}
$
Ddiidev commented 1 year ago

I ran it here in wsl and it really worked for me.

I believe it's only on windows

shuankio commented 1 year ago

this bug is in the https_make_request function of file vschannel.c

line 760: sprintf(pbMessage, "%s", req);

the binary data contains \0 will be truncated

JalonSolov commented 1 year ago

Update to latest V. The problem should be fixed on Windows.

Ddiidev commented 1 year ago

The error continues.

TCC

Start
227463 file ok!
Unhandled Exception 0xC0000005
Unhandled Exception 0xC0000005C
:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:11998: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:11965: by print_backtrace_skipping_top_frames
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:11998: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:12023: by unhandled_exception_handler
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:11965: by print_backtrace_skipping_top_frames
7ffa3b020a6b : by ???
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:12023: by unhandled_exception_handler
7ffa39628ee2 : at ???: RUNTIME ERROR: invalid memory access
C:/v/thirdparty/vschannel/vschannel.c:152: by request
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30997: by net__http__Request_method_and_url_to_response
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:29246: by net__http__Request_ssl_do
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30925: by net__http__Request_do
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30997: by net__http__Request_method_and_url_to_response
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30637: by net__http__fetch
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30579: by net__http__post_multipart_form
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30925: by net__http__Request_do
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:32272: by main__mainC:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30637: by net__http__fetch

C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:30579: by net__http__post_multipart_form
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:32612: by wmain
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:32272: by main__main
0050c968 : by ???
C:/Users/AndreLuiz/AppData/Local/Temp/v_0/http_error.3706575999369906727.tmp.c:32612: by wmain
0050cacb : by ???
0050c968 : by ???
7ffa39381f87 : by ???
0Unhandled Exception 0xC00003740
50cacb : by ???

And here running with GCC

Start
227463 file ok!
Unhandled Exception 0xC0000005
print_backtrace_skipping_top_frames is not implementedUnhandled Exception 0xC0000005

print_backtrace_skipping_top_frames is not implementedsignal 11: segmentation fault
print_backtrace_skipping_top_frames is not implemented

Unhandled Exception 0xC0000374
print_backtrace_skipping_top_frames is not implemented
JalonSolov commented 1 year ago

Actually, that's a different error, now. Perhaps uncovered by fixing the previous.

Ddiidev commented 1 year ago

What do you think is better, mark this as resolved and then open a new issue similar to this one, with this new report?

koplenov commented 1 year ago
Error 0x80090329 returned by EncryptMessage
Unhandled Exception 0xC0000374
C:/Users/koplenov/AppData/Local/Temp/v_0/filer.1399143888674181344.tmp.c:12014: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/koplenov/AppData/Local/Temp/v_0/filer.1399143888674181344.tmp.c:11981: by print_backtrace_skipping_top_frames
C:/Users/koplenov/AppData/Local/Temp/v_0/filer.1399143888674181344.tmp.c:12039: by unhandled_exception_handler
7ff805c08b1c : by ???

I get an error when sending a file

The error is on windows

on linux everything works

koplenov commented 1 year ago

a temporary crutch is to use the system curl

example in my code:

fn upload_file(file_path string, cookie http.Cookie) string {
    println("upload_file: ${file_path}")

    file := os.read_file(file_path) or { panic(err) }
    filename := md5.hexhash(file)

    curl := os.find_abs_path_of_executable('curl') or { panic(err) }

    mut proc := os.new_process(curl)
    proc.set_args([
        '-H "cookie: PHPSESSID=${cookie.value}" -F "filename=${filename}" -F "content=@${file_path}" https://domain.com/admin/include/uploader.php',
    ])
    proc.wait()

    return filename // as hash
}

¯\(ツ)

MCausc78 commented 9 months ago
Error 0x80090329 returned by EncryptMessage
Unhandled Exception 0xC0000374
C:/Users/koplenov/AppData/Local/Temp/v_0/filer.1399143888674181344.tmp.c:12014: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/koplenov/AppData/Local/Temp/v_0/filer.1399143888674181344.tmp.c:11981: by print_backtrace_skipping_top_frames
C:/Users/koplenov/AppData/Local/Temp/v_0/filer.1399143888674181344.tmp.c:12039: by unhandled_exception_handler
7ff805c08b1c : by ???

I get an error when sending a file

The error is on windows

on linux everything works

yeah, it happens sometimes on files larger than 15kb. just pass -d no_vschannel and it will work