Http server구현중 fgets 무한 루프에서 벗어나지 못하는 현상

b2210044의 이미지

HTTP server 구현 중 client로부터 POST된 내용을 모두 가져와서 요구사항에 맞게 파싱해서 사용하려고 하고 있습니다.
문제가 되는 부분은 소켓을 통해 넘어 온 데이타를 읽어오는 부분에서 파일의 끝을 찾지 못하여 무한 루프에서 벗어나지 못하고 있습니다.
http header부분까지는 잘 읽어오나 body부분에서 block된 상태로 멈춰있습니다.
테스트는 Google Postman 이용하고 있고, body는 raw형식으로 전달하고 있습니다.
어느 부분을 확인해봐야 할지 갈피를 잡지 못하여, 조언을 요청 드립니다.

아래는 fgets를 통해 읽어온 부분을 캡처한 것입니다.
packet : POST / HTTP/1.1
packet : Host: 116.126.69.190:8080
packet : Connection: keep-alive
packet : Content-Length: 141
packet : Cache-Control: no-cache
packet : Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
packet : stb_id: 123
packet : Content-Type: text/plain;charset=UTF-8
packet : User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36
packet : Postman-Token: 0b95a0b0-0a2a-a3eb-5233-c20039c0a5f4
packet : Accept: */*
packet : Accept-Encoding: gzip, deflate
packet : Accept-Language: ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
packet :

아래는 코드 일부입니다.
fd는 소켓 파일디스크립터입니다.

        char   packet        [MAX_HEADER_LINE_LENGTH];
        FILE * fr;
 
	fr = fdopen(dup(fd),"r");
 
	if(fr)
	{
		while(!feof(fr))
		{
			fgets(packet, 1024, fr);
		}
        }
bushi의 이미지

"Connection: keep-alive"
+
적고 보니 아닌 듯 합니다.
client 는 보낼 것을 다 보냈고 서버가 그 소켓으로 응답을 해 주길 기다리고 있는 것 같은데요.
http 문서 읽어보시면 client 가 보낸 http request 의 끝지점을 어떻게 인지할 수 있는지 파악이 되실 겁니다.

raymundo의 이미지

POST 데이터는 환경 변수 CONTENT_LENGTH 에 들어있는 길이만큼만 읽도록 해야 되는 걸로 기억합니다.

이 댓글 쓰면서 잠깐 검색해보니, 그 이상 읽으려할 때 서버가 EOF를 준다는 보장이 없다는군요. undefined라고...

http://www.cs.tut.fi/~jkorpela/forms/cgic.html#post

Reading input Reading from standard input sounds probably simpler than reading from an environment variable, but there are complications. The server is not required to pass the data so that when the CGI script tries to read more data than there is, it would get an end of file indi­ca­tion! That is, if you read e.g. using the getchar function in a C program, it is undefined what happens after reading all the data characters; it is not guaranteed that the function will return EOF.

When reading the input, the program must not try to read more than CONTENT_LENGTH characters.


좋은 하루 되세요!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.