ファイルをフォームフィールドcurl
(たとえばcurl -F 'file=@path/to/file' https://example.org/upload
)にアップロードすると、時々設定されるcurl
ことがあります。MIMEタイプMIMEタイプを決定する他のユーティリティによって返されるものとは異なります。
たとえば、.bmp
ビットマップファイルではとfile -i path/to/file.bmp
表示されますが、image/x-ms-bmp
MIMEcurl
タイプはapplication/octet-stream
明示的に上書きしない限り設定されます。
.png
ただし、などの特定のファイル形式では機能します.jpg
。
MIMEタイプをどのように決定するのか、どの条件で期待どおりに機能するのかを知りたいです。
ベストアンサー1
一部のソースコードでは、spelunking forはいくつかのファイル拡張子の一致を実行し、それ以外の場合は奇妙な名前の関数でwhich isがデフォルトにContent-Type
curl
設定されているようです。HTTPPOST_CONTENTTYPE_DEFAULT
application/octet-stream
ContentTypeForFilename
https://github.com/curl/curl/blob/ee56fdb6910f6bf215eecede9e2e9bfc83cb5f29/lib/formdata.c#L166
static const char *ContentTypeForFilename(const char *filename,
const char *prevtype)
{
const char *contenttype = NULL;
unsigned int i;
/*
* No type was specified, we scan through a few well-known
* extensions and pick the first we match!
*/
struct ContentType {
const char *extension;
const char *type;
};
static const struct ContentType ctts[]={
{".gif", "image/gif"},
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".txt", "text/plain"},
{".html", "text/html"},
{".xml", "application/xml"}
};
if(prevtype)
/* default to the previously set/used! */
contenttype = prevtype;
else
contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
if(filename) { /* in case a NULL was passed in */
for(i = 0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
if(strlen(filename) >= strlen(ctts[i].extension)) {
if(strcasecompare(filename +
strlen(filename) - strlen(ctts[i].extension),
ctts[i].extension)) {
contenttype = ctts[i].type;
break;
}
}
}
}
/* we have a contenttype by now */
return contenttype;
}
file(1)
(後でソースコードを修正してタイプマジックチェックを実行できると思いますが、おそらく...)