2014-09-26 7 views
1

나는 내 코드에 문제가있다. 그러나 그것이 옳다! 하지만 내가 그 주소를 잘못 삽입 할 때 : 예를 들어, http://127.0.0.1:27017/this_is_a_try 내 서버가 "이 시도는"이고 "오류 404를 찾을 수 없습니다"라는 메시지로 회신합니다. 그것은 그렇게 어렵습니까? 모두가 도와 줘서 고마워.C - 몽구스 도서관 회신 메시지

#include <signal.h> 
#include <stdlib.h> 
#include "mongoose.h" 
static int s_received_signal = 0; 
static struct mg_server *s_server = NULL; 
static const char *s_remote_addr = "glosbe.com:80"; 
static void signal_handler(int sig_num) { 
    signal(sig_num, signal_handler); 
    s_received_signal = sig_num; 
} 
static int ev_handler(struct mg_connection *conn, enum mg_event ev) { 
    struct mg_connection *client, *orig; 
    switch (ev) { 
    case MG_AUTH: 
     return MG_TRUE; 
    case MG_CONNECT: 
// Send request to the remote host. 
// TODO(lsm): handle connect error here. 
     mg_printf(conn, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", 
       "/gapi/translate?from=eng&dest=fra&format=json&phrase=cat", 
       s_remote_addr); 
     return MG_TRUE; 
    case MG_REPLY: 
// Send reply to the original connection 
     orig = (struct mg_connection *) conn->connection_param; 
     mg_send_header(orig, "Content-Type", "text/plain"); 
     mg_send_data(orig, conn->content, conn->content_len); 
     mg_send_data(orig, "", 0); // Last chunk: mark the end of reply 
// Disconnect connections 
     orig->connection_param = NULL; 
     conn->connection_param = NULL; 
     return MG_TRUE; 
    case MG_REQUEST: 
     if ((client = mg_connect(s_server, s_remote_addr)) != NULL) { 
// Interconnect requests 
      client->connection_param = conn; 
      conn->connection_param = client; 
      return MG_MORE; 
     } else { 
      mg_printf_data(conn, "%s", "cannot send API request"); 
      return MG_TRUE; 
     } 
    default: 
     return MG_FALSE; 
    } 
} 
int main(void) { 
    s_server = mg_create_server(NULL, ev_handler); 
    mg_set_option(s_server, "listening_port", "27017"); 
// Setup signal handlers 
    signal(SIGTERM, signal_handler); 
    signal(SIGINT, signal_handler); 
    printf("Listening on port %s\n", mg_get_option(s_server, "listening_port")); 
    while (s_received_signal == 0) { 
     mg_poll_server(s_server, 1000); 
    } 
    mg_destroy_server(&s_server); 
    printf("Existing on signal %d\n", s_received_signal); 
    return EXIT_SUCCESS; 
} 

답변

0

코드는 몽구스 http_client.c 샘플과 매우 유사합니다.

어쨌든 mongoose 코드를 읽으면 http 응답 코드가 uri 필드를 채우는 것처럼 보입니다. mongoose.c parse_http_message 처리 HTTP 요청에서
다음과 같은 코드로 답 :

ri->request_method = skip(&buf, " "); 
    ri->uri = skip(&buf, " "); 
    ri->http_version = skip(&buf, "\r\n"); 

그런 다음 그것을 사용하여 oportunity을 제공 :

mg_send_status(orig, atoi(conn->uri)); // uri contains status response 

그런 다음 응답 코드의 뜻이된다 뭔가 같은 :

case MG_REPLY: 
    orig = (struct mg_connection *) conn->connection_param; 
    mg_send_status(orig, atoi(conn->uri)); // uri contains status of the http response 
    mg_send_header(orig, "Content-Type", "text/plain"); 
    mg_send_data(orig, conn->content, conn->content_len); 
    mg_send_data(orig, "", 0); // Last chunk: mark the end of reply 
    orig->connection_param = NULL; 
    conn->connection_param = NULL; 
    return MG_TRUE; 

수정 S 이 commit에서보다 명확한 방법으로 http 클라이언트 응답 코드에 액세스 할 수 있습니다.

mg_send_status(orig, conn->status_code);