⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httppost.c

📁 MiniWeb是一个针对嵌入式应用而开发的微型Web Server
💻 C
📖 第 1 页 / 共 2 页
字号:
        else {
          // move to start of next boundary indicator
          pchBoundarySearch = pchEnd;
        }
      }
    }
    
    // Shift to start of next boundary section
    pxMP->iWriteLocation -= (DWORD)pchBoundarySearch - (DWORD)phsSocket->buffer;
    memmove(phsSocket->buffer, pchBoundarySearch, pxMP->iWriteLocation);
    memset(phsSocket->buffer + pxMP->iWriteLocation, 0, HTTPMAXRECVBUFFER - pxMP->iWriteLocation);
    
    // check if this is the last boundary indicator?
    if (strncmp(phsSocket->buffer + strlen(pxMP->pchBoundaryValue) + 2, "--\r\n",4) == 0) {
      // yes, we're all done
      int i;
      
      _mwNotifyPostVars(phsSocket, &(pxMP->pp));
      
      // clear multipart structure
      for (i=0; i<pxMP->pp.iNumParams; i++) {
        free(pxMP->pp.stParams[i].pchParamName);
        free(pxMP->pp.stParams[i].pchParamValue);
      }
      free((HttpMultipart*)phsSocket->ptr);
      (HttpMultipart*)phsSocket->ptr = NULL;
      
      DEBUG("Multipart POST on socket %d complete!\n",
                   phsSocket->socket);
      
      return;
    }
    
    // Search for next boundary indicator
    pchBoundarySearch = _mwFindMultipartBoundary(phsSocket->buffer, 
                                                   HTTPMAXRECVBUFFER, 
                                                   pxMP->pchBoundaryValue);
  }
  
  // check if buffer is full
  if (pxMP->iWriteLocation == HTTPMAXRECVBUFFER) {
    if (pxMP->pchFilename != NULL) {
      // callback with next chunk of posted file
      (*g_httpParam.pfnFileUpload)(pxMP->pchFilename,
                         pxMP->oFileuploadStatus,
                         phsSocket->buffer, 
                         HTTPUPLOAD_CHUNKSIZE);
      pxMP->oFileuploadStatus = HTTPUPLOAD_MORECHUNKS;
      pxMP->iWriteLocation -= HTTPUPLOAD_CHUNKSIZE;
      memmove(phsSocket->buffer, phsSocket->buffer + HTTPUPLOAD_CHUNKSIZE, 
              HTTPMAXRECVBUFFER - HTTPUPLOAD_CHUNKSIZE);
      memset(phsSocket->buffer + HTTPUPLOAD_CHUNKSIZE, 0, HTTPMAXRECVBUFFER - HTTPUPLOAD_CHUNKSIZE);
    } 
    else {
      // error, posted variable too large?
      _mwCloseSocket(phsSocket);
    }
  }
  
  return;
} // end of _mwProcessMultipartPost

////////////////////////////////////////////////////////////////////////////
// _mwProcessPostVars
// Extract and process POST variables
// NOTE: the function damages the recvd data
////////////////////////////////////////////////////////////////////////////
void _mwProcessPostVars(HttpSocket* phsSocket,
                          int iContentOffset,
                          int iContentLength)
{
  BOOL bAuthenticated;
  
#ifdef HTTPAUTH
  bAuthenticated=_mwCheckAuthentication(phsSocket);
#else
  bAuthenticated=TRUE;
#endif

  //ASSERT(iContentOffset+iContentLength<=phsSocket->iDataLength);

  // extract the posted vars
  if (g_httpParam.pfnPost!=NULL) {
    int i;
    char* pchPos;
    char* pchVar=phsSocket->buffer+iContentOffset;
    PostParam pp;
    
    // init number of param block
    memset(&pp, 0, sizeof(PostParam));
    
    // null terminate content data
    *(pchVar+iContentLength)='\0';
    
    // process each param
    for (i=0;i<MAXPOSTPARAMS;i++) {
      // find =
      pchPos=strchr(pchVar,'=');
      if (pchPos==NULL) {
        break;
      }
      // terminate var name and add to parm list
      *pchPos='\0'; 
      pp.stParams[pp.iNumParams].pchParamName=pchVar;
      
      // terminate var value and add to parm list
      pp.stParams[pp.iNumParams].pchParamValue=pchPos+1;
      pchPos=strchr(pchPos+1,'&');
      if (pchPos!=NULL) {
        *pchPos='\0'; // null term current value
      }
      
      // if not authenticated then only process vars starting with .
      if (bAuthenticated || 
          (*pp.stParams[pp.iNumParams].pchParamName=='.')) {
        // convert any encoded characters
        _mwDecodeString(pp.stParams[pp.iNumParams].pchParamValue);
        
        DEBUG("Http POST var %d [%s]=[%s]\n",
               pp.iNumParams,
               pp.stParams[pp.iNumParams].pchParamName,
               pp.stParams[pp.iNumParams].pchParamValue);
        
        pp.iNumParams++;
      } else {
        DEBUG("Http POST var [%s]=[%s] skipped - not authenticated\n",
               pp.stParams[pp.iNumParams].pchParamName,
               pp.stParams[pp.iNumParams].pchParamValue);
      }
      
      // if last var then quit
      if (pchPos==NULL) {
        break;
      }
      
      // move to next var
      pchVar=pchPos+1;
    }

    // process and callback with list of vars
    _mwNotifyPostVars(phsSocket, &pp);

  } else {
    // redirect to index page
    _mwRedirect(phsSocket, "/");
  }
} // end of _mwProcessPostVars

////////////////////////////////////////////////////////////////////////////
// _mwProcessPost
// Process a POST request 
////////////////////////////////////////////////////////////////////////////
void _mwProcessPost(HttpSocket* phsSocket)
{
  int iContentLength=-1;
  int iHeaderLength=0;
  
  //ASSERT(phsSocket->buffer!=NULL);
  
  // null terminate the buffer
  *(phsSocket->buffer+phsSocket->iDataLength)=0;
  
  // find content length
  {
    char* pchContentLength;
    
    pchContentLength=strstr(phsSocket->buffer,
                                       HTTP_CONTENTLENGTH);
    if (pchContentLength!=NULL) {
      pchContentLength+=strlen(HTTP_CONTENTLENGTH);
      iContentLength=atoi(pchContentLength);
    }
  }
  
  // check if content length found
  if (iContentLength>0) {
    
    // check if this is a multipart POST
    if ((HttpMultipart*)phsSocket->ptr == NULL) {
      char *pchMultiPart = _mwStrStrNoCase(phsSocket->buffer, 
                                             HTTP_MULTIPARTHEADER);
      
      if (pchMultiPart != NULL) {
        // We need the full HTTP header before processing (ends in '\r\n\r\n')
        char *pchHttpHeaderEnd = _mwStrDword(phsSocket->buffer, HTTP_HEADEREND, 0);
        
        if (pchHttpHeaderEnd != NULL) {
          char *pchBoundarySearch = NULL;
          int iHttpHeaderLength = (DWORD)pchHttpHeaderEnd + 2 - (DWORD)phsSocket->buffer;
          
          DEBUG("Http multipart POST received on socket %d\n",
                 phsSocket->socket);
          
          // Allocate multipart structure information for socket
          (HttpMultipart*)phsSocket->ptr = calloc(1,sizeof(HttpMultipart));
          //ASSERT((HttpMultipart*)phsSocket->ptr != NULL);
          
          // What is the 'boundary' value
          strcpy((HttpMultipart*)phsSocket->ptr->pchBoundaryValue,"--");
          pchBoundarySearch = _mwStrStrNoCase(phsSocket->buffer, 
                                                HTTP_MULTIPARTBOUNDARY);
          if (pchBoundarySearch != NULL) {
            sscanf(pchBoundarySearch+9,"%s",
                   (HttpMultipart*)phsSocket->ptr->pchBoundaryValue+2);
          } else {
            DEBUG("Error! Http multipart POST header recvd on socket %d does not contain a boundary value\n",
                   phsSocket->socket);
            _mwCloseSocket(phsSocket);
            return;
          }
          
          //ASSERT(phsSocket->buffer != NULL);
          
          // Shift window to start at first boundary indicator
          (HttpMultipart*)phsSocket->ptr->iWriteLocation = 
            phsSocket->iDataLength - iHttpHeaderLength;
          //ASSERT((HttpMultipart*)phsSocket->ptr->iWriteLocation >= 0);
          memmove(phsSocket->buffer, pchHttpHeaderEnd + 2, 
                  (HttpMultipart*)phsSocket->ptr->iWriteLocation);
          memset(phsSocket->buffer + (HttpMultipart*)phsSocket->ptr->iWriteLocation, 0,
                HTTPMAXRECVBUFFER - (HttpMultipart*)phsSocket->ptr->iWriteLocation);
        } 
        else {
          DEBUG("Http multipart POST on socket %d waiting for additional header info\n",
                       phsSocket->socket);
        }
        
        return;
      }
    }
    
    // it's a normal POST. find body of message
    {
      int iLineLength;
      
      do {
        iLineLength=strcspn(phsSocket->buffer+iHeaderLength,"\r\n");
        iHeaderLength+=(iLineLength+2); // move to next line
      } while (iLineLength>0 && iHeaderLength<=phsSocket->iDataLength);
    }
    
    // check if we have the whole message
    if (iHeaderLength+iContentLength <= phsSocket->iDataLength) {
      // process the variables
      _mwProcessPostVars(phsSocket,iHeaderLength,iContentLength);
    } else {
      // not enough content received yet
      DEBUG("Http POST on socket %d waiting for additional data (%d of %d recvd)\n",
                   phsSocket->socket,phsSocket->iDataLength-iHeaderLength,
                   iContentLength);
    }
  } else {
    #if 0
    // header does not contain content length
    SYSLOG(LOG_ERR,"Error! Http POST header recvd on socket %d does not contain content length\n",
           phsSocket->socket);
    #endif
   
  }
} // end of _mwProcessPost
#endif	//HTTPPOST

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -