-
- struct netconn *client;
- struct netbuf *rx_buf_conn;
- char *rx_buf;
- u16_t len;
-
- client = netconn_accept(server);
- kprintf("remote ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
- kprintf("local ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
-
- if (!client)
- continue;
-
- tot_req++;
- rx_buf_conn = netconn_recv(client);
- if (rx_buf_conn)
- {
- netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
- if (rx_buf)
- {
- memset(file_name, 0, sizeof(file_name));
- get_fileName(rx_buf, file_name, sizeof(file_name));
-
- kprintf("%s\n", file_name);
- if (strlen(file_name) == 0)
- strcpy(file_name, "index.htm");
-
- if (!strcmp("bertos_jpg.jpg", file_name))
- {
- netconn_write(client, bertos_jpg, sizeof(bertos_jpg), NETCONN_NOCOPY);
- }
- else if (SD_CARD_PRESENT())
- {
- bool sd_ok = sd_init(&sd, NULL, 0);
- if (sd_ok)
- {
- kprintf("Mount FAT filesystem.\n");
- result = f_mount(0, &fs);
- if (result != FR_OK)
- {
- kprintf("Mounting FAT volumes error[%d]\n", result);
- sd_ok = false;
- f_mount(0, NULL);
- }
-
- if (sd_ok)
- {
- memset(tx_buf, 0, sizeof(tx_buf));
-
- result = fatfile_open(&in_file, file_name, FA_OPEN_EXISTING | FA_READ);
-
- size_t count = 0;
- if (result == FR_OK)
- {
- kprintf("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
-
- netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
-
- while (count < in_file.fat_file.fsize)
- {
- int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
- netconn_write(client, tx_buf, len, NETCONN_COPY);
- count += len;
- }
-
- kfile_flush(&in_file.fd);
- kfile_close(&in_file.fd);
-
- kprintf("Sent: %d\n", count);
- }
- else
- {
- kprintf("Unable to open file: '%s' error[%d]\n", file_name, result);
- netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
- netconn_write(client, http_file_not_found, sizeof(http_file_not_found) - 1, NETCONN_NOCOPY);
- }
- }
- }
- f_mount(0, NULL);
- kprintf("Umount FAT filesystem.\n");
- }
- else
- {
- netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
- netconn_write(client, http_sd_not_present, sizeof(http_sd_not_present), NETCONN_NOCOPY);
- }
- }
- netconn_close(client);
- netbuf_delete(rx_buf_conn);
- }
- netconn_delete(client);