Пример #1
0
unsigned int http_sendfile(unsigned int idx, const char *name, unsigned char *tx)
{
  unsigned int len=0, i;

  if(name) //start transfer -> add http header
  {
    http_table[idx].status = HTTP_SEND;
    http_table[idx].file   = http_fid(name, &http_table[idx].fparam);
    http_table[idx].ftype  = http_ftype(http_table[idx].file);
    http_table[idx].flen   = http_flen(http_table[idx].file, http_table[idx].fparam);
    http_table[idx].fpos   = 0;
    http_table[idx].fparse = 0;

    if(http_table[idx].flen == 0) //file not found
    {
      http_table[idx].status = HTTP_CLOSED;
      len = sprintf((char*)tx, HTTP_404_HEADER"Error 404 Not found\r\n\r\n");
      tcp_send(idx, len, 0);
      tcp_close(idx);
      return len;
    }
    else //file found -> send http header
    {
      switch(http_table[idx].ftype)
      {
        case HTML_FILE:
          len = sprintf((char*)tx, HTTP_HTML_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case XML_FILE:
          len = sprintf((char*)tx, HTTP_XML_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case JS_FILE:
          len = sprintf((char*)tx, HTTP_JS_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case CSS_FILE:
          len = sprintf((char*)tx, HTTP_CSS_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case TXT_FILE:
          len = sprintf((char*)tx, HTTP_TXT_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case ICON_FILE:
          len = sprintf((char*)tx, HTTP_ICON_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case GIF_FILE:
          len = sprintf((char*)tx, HTTP_GIF_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
        case JPEG_FILE:
          len = sprintf((char*)tx, HTTP_JPEG_HEADER"%i\r\n\r\n", http_table[idx].flen);
          tx += len;
          break;
      }
    }
  }

  if(http_table[idx].flen) //file found
  {
    switch(http_table[idx].ftype)
    {
      //dynamic content
      case HTML_FILE:
      case XML_FILE:
        i = http_fparse((char*)tx, http_table[idx].file, &http_table[idx].fparse, (ETH_MTUSIZE-IP_HEADERLEN-TCP_HEADERLEN-MAX_ADDR-100), http_table[idx].fparam);
        http_table[idx].fpos += i;
        len += i;
        break;
      //static content
      case JS_FILE:
      case CSS_FILE:
      case TXT_FILE:
      case ICON_FILE:
      case GIF_FILE:
      case JPEG_FILE:
        i = http_fdata(tx, http_table[idx].file, http_table[idx].fpos, (ETH_MTUSIZE-IP_HEADERLEN-TCP_HEADERLEN-MAX_ADDR-100));
        http_table[idx].fpos += i;
        len += i;
        break;
    }
    tcp_send(idx, len, 0);
    if((http_table[idx].fpos >= http_table[idx].flen) || (len == 0))
    {
      http_close(idx);
      tcp_close(idx);
    }
  }

  return len;
}
unsigned int http_sendfile(unsigned int idx, const char *name, unsigned char *tx)
{
    unsigned int len=0, i = 0;

    if (name) //start transfer -> add http header
    {
        http_table[idx].status = HTTP_SEND;
        http_table[idx].file   = http_fid(name, &http_table[idx].fparam);
        http_table[idx].ftype  = http_ftype(http_table[idx].file);
		http_table[idx].flen   = http_flen(http_table[idx].file, http_table[idx].fparam);
        http_table[idx].fpos   = 0;
        http_table[idx].fparse = 0;

        if(http_table[idx].flen == 0) //file not found
        {
            http_table[idx].status = HTTP_CLOSED;
            len = sprintf((char*)tx, HTTP_404_HEADER"Error 404 Not found\r\n\r\n");
            tcp_send(idx, len);
			log_info("Parse error: 404 Not found.\n");
            return len;
        }
        else //file found -> send http header
        {
            switch(http_table[idx].ftype)
            {
                case HTML_FILE:
                    len = sprintf((char*)tx, HTTP_HTML_HEADER"%i\r\n\r\n", http_table[idx].flen);
                    break;
                case JS_FILE:
                    len = sprintf((char*)tx, HTTP_JS_HEADER"%i\r\n\r\n", http_table[idx].flen);
                    break;
                case CSS_FILE:
                    len = sprintf((char*)tx, HTTP_CSS_HEADER"%i\r\n\r\n", http_table[idx].flen);
                    break;
                case ICON_FILE:
                    len = sprintf((char*)tx, HTTP_ICON_HEADER"%i\r\n\r\n", http_table[idx].flen);
                    break;
           }
           tx += len; 
        }
    }

	log_info("file:%d; ftype:%d; flen:%d\n", 
			http_table[idx].file,
			http_table[idx].ftype,
			http_table[idx].flen);

    if(http_table[idx].flen) //file found
    {
        switch(http_table[idx].ftype)
        {
            //dynamic content
            case HTML_FILE:
            case XML_FILE:
                do {
                    i = http_fparse((char*)tx, http_table[idx].file, &http_table[idx].fparse, 4096, http_table[idx].fparam); 
				   
				   	tx -= len; //重新设置HTTP头,值
				    len = sprintf((char*)tx, HTTP_HTML_HEADER"%i\r\n\r\n", i);
					tx[len] = '<';
					//memcpy(tx,(unsigned char *)rx_buffer,i);                 
				   
				    http_table[idx].fpos += i;
                    len += i;
					log_info("parse len:%d; fpos:%d; fparse:%d; len:%d\n", 
							i, http_table[idx].fpos, http_table[idx].fparse, len);
                    if (len <= 0)
                        break;
                    else
				    {
						//log_info("len:%d == SendData:%d=============\r\n\r\n%s\r\n\r\n=============\r\n\r\n",len,strlen((const char *)SendData),SendData);
                        tcp_send(idx, len);
				     }
                    tx = SendData;
					len = 0;
					log_info("parse len:%d; fpos:%d; fparse:%d; len:%d\n", 
							i, http_table[idx].fpos, http_table[idx].fparse, len);
                } while((http_table[idx].fpos < http_table[idx].flen));
                break;
            case JS_FILE:
            case CSS_FILE:
            case TXT_FILE:
            case ICON_FILE:
            case GIF_FILE:
            case JPEG_FILE:
                do {
                    i = http_fdata(tx, http_table[idx].file, http_table[idx].fpos, 4096); 
                    http_table[idx].fpos += i;
                    len += i;
                    if(len <= 0)
                        break;
                    else
                        tcp_send(idx, len);	
                    tx = SendData;
					len = 0;
                }while((http_table[idx].fpos < http_table[idx].flen));		
                break;
        }
		log_info("after send data: fpos:%d, len:%d\n", http_table[idx].fpos, len);
        if((http_table[idx].fpos >= http_table[idx].flen) || (len == 0))
        {
            http_close(idx);
            log_info("send file OK, close http!\n");
        }
    }

    return len;
}