Changeset 155 for uKadecot/trunk/uip


Ignore:
Timestamp:
Feb 4, 2016, 11:11:09 PM (8 years ago)
Author:
coas-nagasima
Message:

URLがフォルダを指している場合、/index.htmlにリダイレクトするよう変更。

Location:
uKadecot/trunk/uip/apps/webserver
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uKadecot/trunk/uip/apps/webserver/http-strings

    r101 r155  
    88http_content_type "content-type: "
    99http_texthtml "text/html"
    10 http_location "location: "
     10http_location "Location: "
    1111http_host "host: "
    1212http_crnl "\r\n"
     
    2121http_header_101 "HTTP/1.1 101 Switching Protocols\r\n"
    2222http_header_200 "HTTP/1.1 200 OK\r\nServer: uIP/1.0 http://www.toppers.jp/\r\nConnection: close\r\n"
     23http_header_301 "HTTP/1.1 301 Moved Permanently\r\nServer: uIP/1.0 http://www.toppers.jp/\r\nConnection: close\r\n"
    2324http_header_404 "HTTP/1.1 404 Not found\r\nServer: uIP/1.0 http://www.toppers.jp/\r\nConnection: close\r\n"
    2425http_content_encoding_gzip "Content-Encoding: gzip\r\n"
  • uKadecot/trunk/uip/apps/webserver/http-strings.c

    r108 r155  
    2727{0x74, 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, };
    2828const char http_location[11] =
    29 /* "location: " */
    30 {0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, };
     29/* "Location: " */
     30{0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, };
    3131const char http_host[7] =
    3232/* "host: " */
     
    6565/* "HTTP/1.1 200 OK\r\nServer: uIP/1.0 http://www.toppers.jp/\r\nConnection: close\r\n" */
    6666{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0xd, 0xa, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x74, 0x6f, 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, 0x6a, 0x70, 0x2f, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0xd, 0xa, };
     67const char http_header_301[92] =
     68/* "HTTP/1.1 301 Moved Permanently\r\nServer: uIP/1.0 http://www.toppers.jp/\r\nConnection: close\r\n" */
     69{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, 0x33, 0x30, 0x31, 0x20, 0x4d, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x50, 0x65, 0x72, 0x6d, 0x61, 0x6e, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0xd, 0xa, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x74, 0x6f, 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, 0x6a, 0x70, 0x2f, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0xd, 0xa, };
    6770const char http_header_404[84] =
    6871/* "HTTP/1.1 404 Not found\r\nServer: uIP/1.0 http://www.toppers.jp/\r\nConnection: close\r\n" */
  • uKadecot/trunk/uip/apps/webserver/http-strings.h

    r108 r155  
    2121extern const char http_header_101[35];
    2222extern const char http_header_200[77];
     23extern const char http_header_301[92];
    2324extern const char http_header_404[84];
    2425extern const char http_content_encoding_gzip[25];
  • uKadecot/trunk/uip/apps/webserver/httpd-fs.c

    r154 r155  
    4040#include "mmc_rspi.h"
    4141#include <string.h>
     42#include "http-strings.h"
     43
     44#ifndef _MSC_VER
     45#ifndef strcat_s
     46#define strcat_s(dst, dsz, src) strcat(dst, src)
     47#endif
     48#endif
    4249
    4350//#define FILE_DUMP
     
    114121/*-----------------------------------------------------------------------------------*/
    115122int
    116 httpd_fs_open(int drv, const char *name, struct httpd_fs_file *file)
     123httpd_fs_open(int drv, char *name, int len, struct httpd_fs_file *file)
    117124{
    118125        FRESULT res;
    119         FATFS *fs = (FATFS *)file->fs;
     126        FATFS *fs = (FATFS *)&file->fs;
     127        FILINFO fno;
     128        DIR dir;
    120129
    121130        file->pos = 0;
     
    123132        file->drv = drv;
    124133        file->name = name;
     134        file->redirect = 0;
    125135        memset(fs, 0, sizeof(FATFS));
    126136
     
    143153        }
    144154
    145         if((res = pf_open(fs, name)) != FR_OK){
    146                 printf("pf_open(%d:%s) => %d\n", drv, name, res);
    147                 return 0;
     155        if ((res = pf_open(fs, name)) != FR_OK) {
     156                dir.fs = fs;
     157                if ((res = pf_opendir(&dir, name)) != FR_OK) {
     158                        printf("pf_opendir(%d:%s) => %d\n", drv, name, res);
     159                        return 0;
     160                }
     161
     162                if ((res = pf_readdir(&dir, &fno)) != FR_OK) {
     163                        printf("pf_readdir(%d:%s) => %d\n", drv, name, res);
     164                        return 0;
     165                }
     166
     167                if (len != 0/*fno.fattrib & AM_DIR*/) {
     168                        strcat_s(name, len, http_index_html);
     169                        res = pf_open(fs, name);
     170                        file->redirect = res == FR_OK;
     171                }
     172                else
     173                        res = FR_NO_FILE;
     174
     175                if (res != FR_OK) {
     176                        printf("pf_open(%d:%s) => %d %x\n", drv, name, res, fno.fattrib);
     177                        return 0;
     178                }
    148179        }
    149180
     
    160191        FRESULT ret;
    161192        UINT rlen;
    162         FATFS *fs = (FATFS *)file->fs;
     193        FATFS *fs = (FATFS *)&file->fs;
    163194
    164195        if((ret = pf_lseek(fs, file->pos)) != FR_OK){
  • uKadecot/trunk/uip/apps/webserver/httpd-fs.h

    r154 r155  
    4343  int drv;
    4444  const char *name;
    45   void *fs;
     45  int redirect;
     46  char fs[64 + 512/*sizeof(FATFS)*/];
    4647};
    4748
    4849/* file must be allocated by caller and will be filled in
    4950   by the function. */
    50 int httpd_fs_open(int drv, const char *name, struct httpd_fs_file *file);
     51int httpd_fs_open(int drv, char *name, int len, struct httpd_fs_file *file);
    5152int httpd_fs_read(struct httpd_fs_file *file, void *dst, int len);
    5253
  • uKadecot/trunk/uip/apps/webserver/httpd.c

    r154 r155  
    203203  }
    204204
     205  if (s->file.redirect) {
     206    len = strlen(http_location);
     207    memcpy(pos, http_location, len); pos += len;
     208    if (s->drv == 1) {
     209      len = 2;
     210      memcpy(pos, "/~", len); pos += len;
     211    }
     212    len = strlen(s->filename);
     213    memcpy(pos, s->filename, len); pos += len;
     214    len = 2;
     215    memcpy(pos, "\r\n", len); pos += len;
     216  }
     217
    205218  ptr = strrchr(s->message.request_url, ISO_period);
    206219  if (ptr == NULL) {
     
    248261  PT_BEGIN(&s->outputpt);
    249262
    250   s->file.fs = ((char *)&s[1]) - 64 - 512;
    251 
    252   if (!httpd_fs_open(s->drv, s->filename, &s->file)) {
     263  if (!httpd_fs_open(s->drv, s->filename, sizeof(s->filename), &s->file)) {
    253264    s->drv = 0;
    254     httpd_fs_open(s->drv, http_404_html, &s->file);
    255     strcpy_s(s->message.request_url, sizeof(s->message.request_url), http_404_html);
     265    strcpy_s(s->filename, sizeof(s->filename), http_404_html);
     266    httpd_fs_open(s->drv, s->filename, sizeof(s->filename), &s->file);
    256267    PT_WAIT_THREAD(&s->outputpt,
    257268      send_headers(s,
     
    261272    PT_WAIT_THREAD(&s->outputpt,
    262273      send_headers(s,
    263       http_header_200));
     274      s->file.redirect ? http_header_301 : http_header_200));
    264275    PT_WAIT_THREAD(&s->outputpt, send_file(s));
    265276  }
     
    381392  size_t done;
    382393  const char *data;
     394  char *ptr;
     395
    383396  PSOCK_BEGIN(&s->sin);
    384397
     
    407420  /* ""か"/"なら"index.html"に変更 */
    408421  if ((s->message.request_url[0] == '\0') || ((s->message.request_url[0] == '/') && (s->message.request_url[1] == '\0'))) {
    409     strncpy_s(s->message.request_url, sizeof(s->message.request_url), http_index_html, sizeof(s->message.request_url));
    410     s->filename = s->message.request_url;
     422    s->drv = 0;
     423    strcpy_s(s->filename, sizeof(s->filename), http_index_html);
    411424  }
    412425  /* "/~/"ならSDカードから読み込み */
    413426  else if ((s->message.request_url[0] == '/') && (s->message.request_url[1] == '~') && (s->message.request_url[2] == '/')) {
    414427    s->drv = 1;
    415     s->filename = &s->message.request_url[2];
     428    strcpy_s(s->filename, sizeof(s->filename), &s->message.request_url[2]);
    416429  }
    417430  else {
    418431    s->drv = 0;
    419     s->filename = s->message.request_url;
    420   }
     432    strcpy_s(s->filename, sizeof(s->filename), s->message.request_url);
     433  }
     434
     435  ptr = strrchr(s->filename, '?');
     436  if (ptr != NULL)
     437    ptr[0] = '\0';
    421438
    422439  /*  httpd_log_file(uip_conn->ripaddr, s->message.request_url);*/
     
    426443    s->state = STATE_WS_OUTPUT;
    427444
     445    s->close_req = 0;
    428446    websocket_init(&s->websocket, uip_getid((struct uip_conn *)((intptr_t)s - offsetof(struct uip_conn, appstate))));
    429447    for (;;) {
  • uKadecot/trunk/uip/apps/webserver/httpd.h

    r154 r155  
    4747  char inputbuf[50];
    4848  char state;
    49   int drv;
    50   char *filename;
    51   int len;
    52   struct httpd_fs_file file;
    53   int parse_pos;
    54   int parse_len;
    55   struct http_parser parser;
    56   struct http_parser_url handle;
    57   struct message message;
    58   struct websocket websocket;
    59   int close_req;
     49  union{
     50    struct{
     51      int parse_pos;
     52      int parse_len;
     53      struct http_parser parser;
     54      struct http_parser_url handle;
     55      struct message message;
     56    };
     57    struct{
     58      char _dummy1[340];
     59      int drv;
     60      char filename[256];
     61      int len;
     62      struct httpd_fs_file file;
     63    };
     64    struct{
     65      char _dummy2[340];
     66      struct websocket websocket;
     67      int close_req;
     68    };
     69  };
    6070};
    6171#define get_context(p) (struct httpd_state *)((intptr_t)p - (intptr_t)&((struct httpd_state *)0)->parser)
Note: See TracChangeset for help on using the changeset viewer.