C http 서버에서 이미지가 안열리는 문제

tokyo9pm의 이미지

안녕하세요 C로 웹서버를 짜고 있는데 생각만큼 잘 안되네요...

response 만들어서 보내주는 부분인데, content-type이랑 content-length도

request 들어오는 것에 맞춰서 잘 만들어준 것 같은데 웹페이지에서는 이상하게

이미지가 엑스박스로 뜨네요. 직접 이미지 주소 치면 텍스트 형식으로 깨져서 나오구요.

조언 부탁드립니다..

   185  void process_html(int clnt_sock, char* path_name, char* content)
   186  {
   187      FILE * fp;
   188
   189      char protocol[]="HTTP/1.1 200 OK\r\n";
   190      char conn[]="Connection: close\r\n\r\n";
   191      char clength[SMALL_BUF];
   192      char ctype[SMALL_BUF];
   193      char buf[LINESIZE];
   194      int size, count;
   195
   196
   197      if(!(fp=fopen(path_name, "rb")))
   198      {
   199          printf("file not exist:%s\n", path_name);
   200          send_error404(clnt_sock);
   201      }
   202
   203      fseek(fp, 0, SEEK_END);
   204      size = ftell(fp);
   205      rewind(fp);
   206
   207      sprintf(clength, "Content-Length: %d\r\n", size);
   208      sprintf(ctype, "Content-Type: %s\r\n", content);
   209
   210      send(clnt_sock, protocol, strlen(protocol)+1, 0);
   211      send(clnt_sock, ctype, strlen(ctype)+1, 0);
   212      send(clnt_sock, clength, strlen(clength)+1, 0);
   213      send(clnt_sock, conn, strlen(conn)+1, 0);
   214
   215      while((count = fread(buf, 1, LINESIZE, fp)) > 0)
   216      {
   217          write(clnt_sock, buf, count);
   218      }
   219
   220      close(clnt_sock);
   221      fclose(fp);
   222  }
MyAbby의 이미지

strlen() + 1을 하는데, 음... 헤더에 NULL문자가 들어가서 브라우저가 당황했겠내요.
헤더는:

상태\r\n
헤더1\r\n
헤더2\r\n
...
\r\n
(본문)

이렇게 하면 브라우저가 잘 이해해요. 왜 굳지 0을 보내는지 모르겟군요.

이미지가 텍스트로 나오는 거는 content-type 이 이상해서 그럴거에요. 이미지의 magic을 계산하는 방식을 어떻게 구현하셨는지 잘 모르겠습니다.
리눅스 명령에 파일의 mime 을 계산해 주는 명령이: file -i <파일> 입니다.
system 호출을 해서 알아낸다면 그 부분을 확인하셔요. 아니면 저 file 라이브러리를 포팅해서 쓰신다면 그쪽 반환값을 확인해 보세요.

x

tokyo9pm의 이미지

덕분에 이미지 문제도 해결했고, 크롬에서 페이지 접근 안되는 문제도 다 저 부분이 문제였네요.
content-type은 단순히 파일이름에서 확장자만 떼어서 판단했는데, 언급하신 방법 대로 하는 것이 좋을 것 같습니다.
답변 감사드립니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.