socket fd를 통해 chunked 데이터 처리 관련 질문

matt104의 이미지

docker의 unix socket을 통해 event를 받아오려고 하는데 자꾸 chunked data가 0이 나오네요..;;

unix-socket(/var/run/docker.sock)과 connection 맺은 다음에 해당 fd를 epoll에 등록,
socket에 data가 올때마다 data를 읽으려고 했는데..
계속 socket read를 하면 chunked data의 length가 0이 나오고 호출한 api에 대한 응답이 끊기는것 같습니다.
원래 Transfer-Encoding: chunked일 때 0이 나오면 끝나는 것은 알고 있는데 왜 connection(?http connection)이 유지되지 못하고 0을 반환하고 끝나버리는지 도저히 모르겠네요..

socket 관련 설정을 아무리 변경해봐도 동작을 안합니다.
socket을 nonblocking socket으로 변경도 해보고, setsockopt으로 SO_KEEPALIVE 옵션도 줘봤는데
뭔가 document를 읽어봐도 이게 문제는 아닌것 같아서 질문 드립니다.

struct sockaddr_un server;
server.sun_family = AF_UNIX;
std::strcpy(server.sun_path, "/var/run/docker.sock");
 
if(connect(docker_fd, (struct sockaddr*) &server, sizeof(struct sockaddr_un)) < 0) {
    close(docker_fd);
    perror("failed to connect to docker.sock");                                                                            
    exit(EXIT_FAILURE);
}   
 
int keep_alive = 1;
if(setsockopt(docker_fd, SOL_SOCKET, SO_KEEPALIVE, &keep_alive, sizeof(keep_alive)) < 0){ 
    perror("failed");
}   
 
int flag = fcntl(docker_fd, F_GETFL, 0); 
if(fcntl(docker_fd, F_SETFL, flag | O_NONBLOCK) < 0){ 
    perror("failed");   
};  
 
char msg[] = "GET /events HTTP/1.1\nHost: docker\nAccept: */*\n\n";
if(send(docker_fd, msg, sizeof(msg), 0) < 0) {
    close(docker_fd);
    perror("failed");
}


events api를 보내놓고 응답이 왔을 때 다음과 같이 read를 했습니다..
char buffer[1024];
int temp = read(docker_fd, buffer, sizeof(buffer)-1);
if(temp < 0){ 
    close(docker_fd);
    perror("failed");
}  

이러고 buffer를 찍어보면 chunked가 0이 찍히고 끝나버리네요..

HTTP/1.1 200 OK^MApi-Version: 1.39
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/18.09.3 (linux)
Date: Tue, 30 Jul 2019 08:50:12 GMT
Transfer-Encoding: chunked
 
0

Transfer-Encoding: chunked 까지만 나오고 끝나야 맞을 것 같은데 0이..;;
chunked까지만 찍히고 그 이후에 버퍼에 쌓였을 때 epoll event 발생,
그 때 다시 소켓버퍼를 read하고 싶습니다.

뭔가 설정을 잘못했거나,
혹은 read를 하는 시점에 원래 connection(?)이 끊기고 streaming이 종료되는건지 궁금합니다.

고수님들 답변 부탁드려요~ㅠ

댓글 달기

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