Linux下HTTP Server

來源:互聯網
上載者:User

標籤:log   send   return   net   size   oid   http請求   內容   base   

想在Linux下實現一個簡單的web Server並不難。一個最簡單的HTTP Server不過是一個進階的檔案伺服器,不斷地接收用戶端(瀏覽器)發送的HTTP請求,解析請求,處理請求,然後像用戶端回送資料。在大多是情況下,(GET、POST命令),服務求回傳給用戶端的都是檔案(HTML 文檔, 圖片,JavaScript指令碼等等)。

下面是一個極簡單的HTTP Server的demo,雖然只處理GET請求並發送單一檔案,但基本展示了web server的架構。我的例子試圖將功能和結構做到最精簡,這樣,一個http server的基本結構,便一目瞭然。

[cpp] view plain copy 
  1. #include<sys/socket.h>  
  2. #include<errno.h>  
  3. #include<netinet/in.h>  
  4. #include<string.h>  
  5. #include<stdio.h>  
  6.   
  7. #define BUF_LEN 1028  
  8. #define SERVER_PORT 8080  
  9.   
  10. //定義好的html頁面,實際情況下web server基本是從本地檔案系統讀取html檔案   
  11. const static char http_error_hdr[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n";  
  12. const static char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";  
  13. const static char http_index_html[] =   
  14. "<html><head><title>Congrats!</title></head>"  
  15. "<body><h1>Welcome to our HTTP server demo!</h1>"  
  16. "<p>This is a just small test page.</body></html>";  
  17.   
  18. //解析到HTTP請求的檔案後,發送本地檔案系統中的檔案  
  19. //這裡,我們處理對index檔案的請求,發送我們預定好的html檔案  
  20. //呵呵,一切從簡!   
  21. int http_send_file(char *filename, int sockfd)  
  22. {  
  23.     if(!strcmp(filename, "/")){  
  24.         //通過write函數發送http響應報文;報文包括HTTP回應標頭和響應內容--HTML檔案   
  25.         write(sockfd, http_html_hdr, strlen(http_html_hdr));  
  26.         write(sockfd, http_index_html, strlen(http_index_html));  
  27.     }  
  28.     else{  
  29.          // 檔案未找到情況下發送404error響應   
  30.         printf("%s:file not find!\n",filename);  
  31.         write(sockfd, http_error_hdr, strlen(http_error_hdr));  
  32.     }  
  33.   return 0;  
  34. }  
  35.   
  36. //HTTP請求解析   
  37. void serve(int sockfd){  
  38.     char buf[BUF_LEN];  
  39.     read(sockfd, buf, BUF_LEN);  
  40.     if(!strncmp(buf, "GET", 3)){  
  41.         char *file = buf + 4;  
  42.         char *space = strchr(file, ‘ ‘);  
  43.         *space = ‘\0‘;  
  44.         http_send_file(file, sockfd);  
  45.     }  
  46.     else{  
  47.          //其他HTTP請求處理,如POST,HEAD等 。這裡我們只處理GET   
  48.         printf("unsupported request!\n");  
  49.         return;  
  50.     }  
  51. }  
  52.   
  53. void main(){  
  54.     int sockfd,err,newfd;  
  55.     struct sockaddr_in addr;  
  56.     //建立TCP通訊端   
  57.     sockfd = socket(AF_INET, SOCK_STREAM, 0);  
  58.     if(sockfd < 0){  
  59.         perror("socket creation failed!\n");  
  60.         return;  
  61.     }  
  62.     memset(&addr, 0, sizeof(addr));  
  63.     addr.sin_family = AF_INET;  
  64.     //這裡要注意,連接埠號碼一定要使用htons先轉化為網路位元組序,否則綁定的實際連接埠  
  65.     //可能和你需要的不同   
  66.     addr.sin_port = htons(SERVER_PORT);  
  67.     addr.sin_addr.s_addr = INADDR_ANY;  
  68.     if(bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))){  
  69.         perror("socket binding failed!\n");  
  70.         return;  
  71.     }  
  72.     listen(sockfd, 128);  
  73.     for(;;){  
  74.         //不間斷接收HTTP請求並處理,這裡使用單線程,在實際情況下考慮到效率一般多線程   
  75.         newfd = accept(sockfd, NULL, NULL);  
  76.         serve(newfd);  
  77.         close(newfd);  
  78.     }  
  79. }  

Linux下HTTP Server

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.