文件上传并使用LightTPD存储
我在嵌入式平台上在Linux中运行LighthTTPD。
现在,我想通过包含文件选择器和“上传”按钮的上传网页将文件传输到系统(带有HTML标签和)。选定的文件作为包含Multipart/form-data的Post http请求传输。然后,该文件应简单地作为常规文件存储在文件系统中。
我已经有一个CGI接口,一个接收请求并将其传递给后端C ++应用程序的BASH脚本。而且由于它是一个嵌入式平台,所以我想避免使用PHP,Python等。仅在这种情况下。
据我所知,LightTPD无法将接收到的文件直接从Multipart编码的请求主体保存到纯文件中,对吗?
为了解码身体,我从MPACK软件包中找到了“ MunPack”工具,该工具将编码的正文写入磁盘上的文件,但旨在用于MIME编码的电子邮件。尽管如此,我可以在CGI bash脚本中称其为“终止边界ID”(在两个破折号附加的“ content-type”中给出的边界ID),这是最后一次文件仍包含最终边界。 更新:这种MunPack的行为来自一个错误的脚本,但是当身体包含CRLF线路结尾时,MunPack仍然会产生错误的文件;只有LF产生正确的结果。
还有其他直接要求在烟道上的请求吗?还是我真的必须在脚本中手动过滤终止边界,还是在我的C ++应用程序中编写多部分 - 消息解析器?
为了清楚用例:用户应该能够将固件文件上传到我的系统。因此,他使用Web浏览器连接到我的系统,收到一个上传页面,可以在其中选择文件并使用“上传”按钮发送。然后,该传输的文件应简单地存储在我的系统上。用于接收该请求的CGI脚本确实已经存在(以及我也可以处理请求的C ++后端),唯一的问题是将Multipart/form-data编码文件转换为磁盘上的普通文件。
I am running lighthttpd in Linux on an embedded platform.
Now i want to make it possible to transfer a file to the system, with an upload web page containing a file selector and "Upload" button (with HTML tags and ). The selected file is transferred as a POST HTTP request containing multipart/form-data. The file should then simply be stored as a regular file in the file system.
I'm already having a CGI interface, a bash script which receives the request and which passes it to the backend C++ application. And because it is an embedded platform, i would like to avoid using php, python etc. only for this case.
As far as i see, lighttpd is not able to save the received files directly from multipart-encoded request body to pure files, correct?
To decode the body i found 'munpack' tool from the mpack package, which writes the encoded body to files on disk, but is intended for mime encoded emails. Nevertheless i can call it in the CGI bash script, and it works almost like expected, except that it can't handle the terminating boundary id (the boundary id given in 'Content-Type' appended by two dashes), resulting in the last file still containing the final boundary. Update: This munpack behaviour came from a faulty script, but still it doesn't work, munpack produces wrong files when the body contains CRLF line endings; only LF produces the correct result.
Is there any other direct request-to-file-on-disk approach? Or do i really have to filter out the terminating boundary manually in the script, or write a multipart-message parser in my C++ application?
To make the use case clear: A user should be able to upload a firmware file to my system. So he connects to my system with a web browser, receives an upload page where he can select the file and send it with an "Upload" button. This transferred file should then simply be stored on my system. The CGI script for receiving the request does already exist (as well as a C++ backend where i could handle the request, too), the only problem is converting the multipart/form-data encoded file to a plain file on disk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来更像是HTTP放置,而不是HTTP帖子。
您是说应用程序/x-www-form-urlCorm-urlenCod用帖子编码吗?
为什么要编码多部分?是否有多个文件上传?
lighttpd mod_webdav 支持。否则,您需要自己的程序来处理请求主体,无论是外壳脚本还是编译程序。您可以将libfcgi与C ++一起使用,也可以查看LightTPD用于测试的C程序,这些程序在&lt in&lt in<每条C的300行。
That sounds more like it should be an HTTP PUT rather than an HTTP POST.
Do you mean application/x-www-form-urlencoded with the POST?
Why multipart-encoded? Are there multiple files being uploaded?
lighttpd mod_webdav supports PUT. Otherwise, you need your own program to handle the request body, be it a shell script or a compiled program. You can use libfcgi with your C++, or you can look at the C programs that lighttpd uses for testing, which implement FastCGI and SCGI in < 300 lines of C each.