2017-03-02 193 views
0

我想用mongoose C庫開發一個服務器應用程序。在我嘗試的初始階段,我堅持發送HTTP請求的響應。我想送status 200一個簡單的響應使用的代碼下面一行:mongoose mg_send_response_line()不工作

mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); 
printf("Response sent...\n"); 

但反應未接收到客戶端(郵差或web瀏覽器)。 沒有錯誤,甚至是printf line of Response sent is printed。 作爲針對這一點,以下行得到成功執行:

mg_http_send_error(nc,404, "Fatal Error!"); // I get this error at client side. 

基本simplest_web_server也能正常工作。爲什麼我的單行代碼發送響應失敗。我無法理解/調試它。

Regards,

Neeraj。

回答

0

問題是沒有爲HTTP響應指定內容長度或傳輸編碼,並且服務器未關閉連接,因此客戶端掛起等待響應。

如果你去翻source code,你會看到,在mg_http_send_error(),該MG_F_SEND_AND_CLOSE標誌設置,但它不是內mg_send_response_line()設置(雖然,像你這樣的,我認爲這將通過函數處理)。

要解決在上下文中的問題,

mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); 
printf("Response sent...\n"); 
nc->flag |= MG_F_SEND_AND_CLOSE;