명령줄의 "curl"은 업로드되는 파일의 MIME 유형을 어떻게 결정합니까?

명령줄의 "curl"은 업로드되는 파일의 MIME 유형을 어떻게 결정합니까?

파일을 양식 필드 curl(예 curl -F 'file=@path/to/file' https://example.org/upload: ) 로 업로드할 때 curl가끔 설정되는 경우가 있습니다.MIME 유형MIME 유형을 결정하는 다른 유틸리티에서 반환되는 것과는 다릅니다.

예를 들어, .bmp비트맵 파일에서는 file -i path/to/file.bmp이라고 표시되지만 image/x-ms-bmpMIME curl유형은 application/octet-stream명시적으로 재정의하지 않는 한 으로 설정됩니다.

.png그러나 및 와 같은 특정 파일 형식에서는 작동합니다 .jpg.

MIME 유형을 어떻게 결정하는지, 어떤 조건에서 예상대로 작동하는지 알고 싶습니다.

답변1

일부 소스 코드에서 spelunking for는 일부 파일 확장자 일치를 수행하고 그렇지 않으면 이상한 이름의 함수에서 which is가 Content-Type curl기본값으로 설정되는 것 같습니다 .HTTPPOST_CONTENTTYPE_DEFAULTapplication/octet-streamContentTypeForFilename

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)(나중에 소스 코드를 수정하여 유형 마법 검사를 수행할 수 있다고 생각하지만 아마도...)

관련 정보