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

📄 httppost.c

📁 小型的web服务器源码
💻 C
📖 第 1 页 / 共 2 页
字号:
    // Shift to start of next boundary section    pxMP->iWriteLocation -= (DWORD)pchBoundarySearch - (DWORD)phsSocket->pucData;    memmove(phsSocket->pucData, pchBoundarySearch, pxMP->iWriteLocation);    memset(phsSocket->pucData + pxMP->iWriteLocation, 0,          HTTPMAXRECVBUFFER - pxMP->iWriteLocation);        // check if this is the last boundary indicator?    if (strncmp(phsSocket->pucData + 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(phsSocket->pxMultipartInfo);      phsSocket->pxMultipartInfo = NULL;            DEBUG("Multipart POST on socket %d complete!\n",                   phsSocket->iSocket);            return;    }        // Search for next boundary indicator    pchBoundarySearch = _mwFindMultipartBoundary(phsSocket->pucData,                                                    HTTPMAXRECVBUFFER,                                                    pxMP->pchBoundaryValue);  }    // check if buffer is full  if (pxMP->iWriteLocation == HTTPMAXRECVBUFFER) {    if (pxMP->pchFilename != NULL) {      // callback with next chunk of posted file      (*g_pfnFileUpload)(pxMP->pchFilename,                         pxMP->oFileuploadStatus,                         phsSocket->pucData,                          HTTPUPLOAD_CHUNKSIZE);      pxMP->oFileuploadStatus = HTTPUPLOAD_MORECHUNKS;      pxMP->iWriteLocation -= HTTPUPLOAD_CHUNKSIZE;      memmove(phsSocket->pucData, phsSocket->pucData + HTTPUPLOAD_CHUNKSIZE,               HTTPMAXRECVBUFFER - HTTPUPLOAD_CHUNKSIZE);      memset(phsSocket->pucData + 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=_mwCheckAuthentication(phsSocket);  //ASSERT(iContentOffset+iContentLength<=phsSocket->iDataLength);  // extract the posted vars  if (g_pfnPost!=NULL) {    int i;    char* pchPos;    char* pchVar=phsSocket->pucData+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->pucData!=NULL);    // null terminate the buffer  *(phsSocket->pucData+phsSocket->iDataLength)=0;    // find content length  {    char* pchContentLength;        pchContentLength=strstr(phsSocket->pucData,                                       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 (phsSocket->pxMultipartInfo == NULL) {      char *pchMultiPart = _mwStrStrNoCase(phsSocket->pucData,                                              HTTP_MULTIPARTHEADER);            if (pchMultiPart != NULL) {        // We need the full HTTP header before processing (ends in '\r\n\r\n')        char *pchHttpHeaderEnd = _mwStrDword(phsSocket->pucData, HTTP_HEADEREND, 0);                if (pchHttpHeaderEnd != NULL) {          char *pchBoundarySearch = NULL;          int iHttpHeaderLength = (DWORD)pchHttpHeaderEnd + 2 - (DWORD)phsSocket->pucData;                    DEBUG("Http multipart POST received on socket %d\n",                 phsSocket->iSocket);                    // Allocate multipart structure information for socket          phsSocket->pxMultipartInfo = calloc(1,sizeof(HttpMultipart));          //ASSERT(phsSocket->pxMultipartInfo != NULL);                    // What is the 'boundary' value          strcpy(phsSocket->pxMultipartInfo->pchBoundaryValue,"--");          pchBoundarySearch = _mwStrStrNoCase(phsSocket->pucData,                                                 HTTP_MULTIPARTBOUNDARY);          if (pchBoundarySearch != NULL) {            sscanf(pchBoundarySearch+9,"%s",                   phsSocket->pxMultipartInfo->pchBoundaryValue+2);          } else {            DEBUG("Error! Http multipart POST header recvd on socket %d does not contain a boundary value\n",                   phsSocket->iSocket);            _mwCloseSocket(phsSocket);            return;          }                    // Allocate memory window buffer for multipart POST          phsSocket->pucData = realloc(phsSocket->pucData,                                        HTTPMAXRECVBUFFER);          //ASSERT(phsSocket->pucData != NULL);                    // Shift window to start at first boundary indicator          phsSocket->pxMultipartInfo->iWriteLocation =             phsSocket->iDataLength - iHttpHeaderLength;          //ASSERT(phsSocket->pxMultipartInfo->iWriteLocation >= 0);          memmove(phsSocket->pucData, pchHttpHeaderEnd + 2,                   phsSocket->pxMultipartInfo->iWriteLocation);          memset(phsSocket->pucData + phsSocket->pxMultipartInfo->iWriteLocation, 0,                HTTPMAXRECVBUFFER - phsSocket->pxMultipartInfo->iWriteLocation);        }         else {          DEBUG("Http multipart POST on socket %d waiting for additional header info\n",                       phsSocket->iSocket);        }                return;      }    }        // it's a normal POST. find body of message    {      int iLineLength;            do {        iLineLength=strcspn(phsSocket->pucData+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->iSocket,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->iSocket);    #endif     }} // end of _mwProcessPost#endif

⌨️ 快捷键说明

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