Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 5956

how to deal with multipart form data in bash?

$
0
0

Backend running on thttpd using cgi. Decide to add file upload feature.

<form action="index.cgi" method="post" enctype="multipart/form-data"><input type="file" name="fileToUpload" id="fileToUpload"><input type="submit" value="Upload File" name="submit"></form>

then the part thats reading stdin and writing to a file

if [ "$REQUEST_METHOD" == "POST" ]; thenwhile IFS= read -r line; doprintf "%s" "$line" >> encoded_filedonefi

I have no issues with text based files the only issue is with binary data i.e. png file in my case

using hexedit to view the file:

00000000   2D 2D 2D 2D  2D 2D 2D 2D  2D 2D 2D 2D  2D 2D 2D 2D  2D 2D 2D 2D  2D 2D 2D 2D  ------------------------00000018   2D 2D 2D 2D  2D 31 36 39  33 34 34 35  39 32 39 31  34 35 30 36  31 30 30 36  -----169344592914506100600000030   35 34 31 34  31 35 32 32  31 32 0D 43  6F 6E 74 65  6E 74 2D 44  69 73 70 6F  5414152212.Content-Dispo00000048   73 69 74 69  6F 6E 3A 20  66 6F 72 6D  2D 64 61 74  61 3B 20 6E  61 6D 65 3D  sition: form-data; name=00000060   22 66 69 6C  65 54 6F 55  70 6C 6F 61  64 22 3B 20  66 69 6C 65  6E 61 6D 65  "fileToUpload"; filename00000078   3D 22 49 4D  47 5F 37 30  39 37 2E 50  4E 47 22 0D  43 6F 6E 74  65 6E 74 2D  ="IMG_7097.PNG".Content-00000090   54 79 70 65  3A 20 69 6D  61 67 65 2F  70 6E 67 0D  0D 89 50 4E  47 0D 1A 0D  Type: image/png...PNG...000000A8   49 48 44 52  01 7B 03 93  08 06 5E B6  A3 CD 01 73  52 47 42 AE  CE 1C E9 38  IHDR.{....^....sRGB....8000000C0   65 58 49 66  4D 4D 2A 08  01 87 69 04  01 1A 02 A0  02 04 01 01  7B A0 03 04  eXIfMM*...i.........{...000000D8   01 03 93 4E  72 1E D1 40  49 44 41 54  78 01 EC BD  07 94 5E C7  75 E7 F9 BE  ...Nr..@IDATx.....^.u...

passing thru uudeview to get rid of boundaries and header

00000000   89 50 4E 47  0A 1A 0A 49  48 44 52 01  7B 03 93 08  06 5E B6 A3  CD 01 73 52  .PNG...IHDR.{....^....sR00000018   47 42 AE CE  1C E9 38 65  58 49 66 4D  4D 2A 08 01  87 69 04 01  1A 02 A0 02  GB....8eXIfMM*...i......00000030   04 01 01 7B  A0 03 04 01  03 93 4E 72  1E D1 40 49  44 41 54 78  01 EC BD 07  ...{......Nr..@IDATx....00000048   94 5E C7 75  E7 F9 BE 9C  FA EB 1C D0  88 4D 10 19  CC 99 14 65  CB E3 75 D8  .^.u.........M.....e..u.00000060   B1 C7 B3 92  C7 E3 73 BC  5A 7B 25 79  EC B5 CE D9  D9 9D B4 3E  67 45 EE CC  ......s.Z{%y.......>gE..00000078   1C CF 7A 1D  24 4A 1C 5B  B2 C7 92 AC  51 22 6D 25  52 A2 25 91  22 44 93 12  ..z.$J.[....Q"m%R.%."D..00000090   13 C4 02 24  52 A3 81 6E  34 42 E7 EE  2F C7 FD FF  6E BD D7 FD  01 04 29 59  ...$R..n4B../...n.....)Y

hex of original file

00000000   89 50 4E 47  0D 0A 1A 0A  00 00 00 0D  49 48 44 52  00 00 01 7B  00 00 03 93  .PNG........IHDR...{....00000018   08 06 00 00  00 5E B6 A3  CD 00 00 00  01 73 52 47  42 00 AE CE  1C E9 00 00  .....^.......sRGB.......00000030   00 38 65 58  49 66 4D 4D  00 2A 00 00  00 08 00 01  87 69 00 04  00 00 00 01  .8eXIfMM.*.......i......00000048   00 00 00 1A  00 00 00 00  00 02 A0 02  00 04 00 00  00 01 00 00  01 7B A0 03  .....................{..00000060   00 04 00 00  00 01 00 00  03 93 00 00  00 00 4E 72  1E D1 00 00  40 00 49 44  ..............Nr....@.ID00000078   41 54 78 01  EC BD 07 94  5E C7 75 E7  F9 BE 9C FA  EB 1C D0 88  4D 10 19 CC  ATx.....^.u.........M...00000090   99 0A 14 65  CB E3 75 D8  B1 C7 B3 92  C7 E3 73 BC  5A 7B 25 79  EC B5 CE D9  ...e..u.......s.Z{%y....000000A8   D9 9D B4 3E  67 45 EE CC  1C CF 7A 1D  24 4A 1C 5B  B2 C7 92 AC  51 22 6D 25  ...>gE....z.$J.[....Q"m%000000C0   52 A2 25 91  22 44 93 12  13 C4 00 02  24 52 A3 81  6E 34 42 E7  EE 2F C7 FD  R.%."D......$R..n4B../..

How to get actual original file at the backend with bash ?

Update: @pjh suggestion using cat worked in sense of preserving null bytes. But extracting content still doesnt work with uudeview or manually. As soon as im “touching” the file to change anything im loosing null bytes.


Viewing all articles
Browse latest Browse all 5956

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>