MyErrorCode HttpConnect::ProcessRequest(const HttpRequest& cRequest, HttpResponse& cResponse) { MyErrorCode eResult = E_INVALID_REQ; switch (cRequest.m_eReqType) { case HttpRequest::HTTP_GET: eResult = ProcessGetRequest(cRequest, cResponse); break; case HttpRequest::HTTP_POST: eResult = ProcessPostRequest(cRequest, cResponse); break; default: eResult = E_INVALID_REQ; break; } #ifdef _DEBUG std::cout << "Request url:" << std::endl << "\t" << cRequest.m_strUrl << std::endl << "Response:" << std::endl << "\t" << cResponse.m_strResponse << std::endl << std::endl; #endif return eResult; }
// SIGINT alarm handler: alarm set by entity handler. Does // slow response when fired void AlarmHandler(int sig) { if (sig == SIGALRM) { OC_LOG (INFO, TAG, "Server starting slow response"); if (gRequestList.empty()) { OC_LOG (INFO, TAG, "No requests to service"); return; } // Get the request from the list OCEntityHandlerRequest *entityHandlerRequest = gRequestList.front(); gRequestList.pop_front(); if (entityHandlerRequest->method == OC_REST_GET) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ProcessGetRequest (entityHandlerRequest); } else { OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); } // Free the request OCFree(entityHandlerRequest->query); OCFree(entityHandlerRequest->reqJSONPayload); OCFree(entityHandlerRequest); // If there are more requests in list, re-arm the alarm signal if (gRequestList.empty()) { alarm(SLOW_RESPONSE_DELAY_SEC); } } }
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam) { OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); (void)callbackParam; OCEntityHandlerResult ehResult = OC_EH_ERROR; OCEntityHandlerResponse response; memset(&response, 0, sizeof(response)); // Validate pointer if (!entityHandlerRequest) { OIC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } OCRepPayload* payload = NULL; if (flag & OC_REQUEST_FLAG) { OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (entityHandlerRequest) { if (OC_REST_GET == entityHandlerRequest->method) { OIC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ehResult = ProcessGetRequest (entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ehResult = ProcessPutRequest (entityHandlerRequest, &payload); } else if (OC_REST_POST == entityHandlerRequest->method) { OIC_LOG (INFO, TAG, "Received OC_REST_POST from client"); ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload); } else { OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); ehResult = OC_EH_ERROR; } if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (OCPayload*)(payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof(response.sendVendorSpecificHeaderOptions)); memset(response.resourceUri, 0, sizeof(response.resourceUri)); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Error sending response"); ehResult = OC_EH_ERROR; } } } } OCPayloadDestroy(response.payload); return ehResult; }
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest) { OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); OCEntityHandlerResult ehResult = OC_EH_OK; OCEntityHandlerResponse response; char payload[MAX_RESPONSE_LENGTH] = {0}; // Validate pointer if (!entityHandlerRequest) { OC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } // Initialize certain response fields response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); if (flag & OC_INIT_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG"); } if (flag & OC_REQUEST_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_POST == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_POST from client"); ehResult = ProcessPostRequest (entityHandlerRequest, &response, payload, sizeof(payload) - 1); } else if (OC_REST_DELETE == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client"); ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else { OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); } // If the result isn't an error or forbidden, send response if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN))) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (unsigned char *)payload; response.payloadSize = strlen(payload); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Handle vendor specific options if(entityHandlerRequest->rcvdVendorSpecificHeaderOptions && entityHandlerRequest->numRcvdVendorSpecificHeaderOptions) { OC_LOG (INFO, TAG, "Received vendor specific options"); uint8_t i = 0; OCHeaderOption * rcvdOptions = entityHandlerRequest->rcvdVendorSpecificHeaderOptions; for( i = 0; i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions; i++) { if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID) { OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with", ((OCHeaderOption)rcvdOptions[i]).optionID ); OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData, ((OCHeaderOption)rcvdOptions[i]).optionLength); } } OCHeaderOption * sendOptions = response.sendVendorSpecificHeaderOptions; uint8_t option2[] = {21,22,23,24,25,26,27,28,29,30}; uint8_t option3[] = {31,32,33,34,35,36,37,38,39,40}; sendOptions[0].protocolID = OC_COAP_ID; sendOptions[0].optionID = 2248; memcpy(sendOptions[0].optionData, option2, sizeof(option2)); sendOptions[0].optionLength = 10; sendOptions[1].protocolID = OC_COAP_ID; sendOptions[1].optionID = 2600; memcpy(sendOptions[1].optionData, option3, sizeof(option3)); sendOptions[1].optionLength = 10; response.numSendVendorSpecificHeaderOptions = 2; } // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehResult = OC_EH_ERROR; } } } if (flag & OC_OBSERVE_FLAG) { OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG"); if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client"); ProcessObserveRegister (entityHandlerRequest); } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client"); ProcessObserveDeregister (entityHandlerRequest); } } return ehResult; }
OCEntityHandlerResult OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, char* uri) { OC_LOG_V (INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri); OCEntityHandlerResult ehResult = OC_EH_OK; OCEntityHandlerResponse response; char payload[MAX_RESPONSE_LENGTH] = {0}; // Validate pointer if (!entityHandlerRequest) { OC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } // Initialize certain response fields response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); if (flag & OC_INIT_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG"); } if (flag & OC_REQUEST_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (entityHandlerRequest->resource == NULL) { OC_LOG (INFO, TAG, "Received request from client to a non-existing resource"); ehResult = ProcessNonExistingResourceRequest(entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_DELETE == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client"); ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else { OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); ehResult = OC_EH_ERROR; } // If the result isn't an error or forbidden, send response if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN))) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (unsigned char *)payload; response.payloadSize = strlen(payload); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehResult = OC_EH_ERROR; } } } if (flag & OC_OBSERVE_FLAG) { OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG"); if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client"); } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client"); } } return ehResult; }
/** * This is the entity handler for the registered resource. * This is invoked by OCStack whenever it recevies a request for this resource. */ OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest* entityHandlerRequest, void *callback) { (void) callback; OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { }, { 0 }, false }; OCRepPayload* payload = NULL; if (entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { if (OC_REST_GET == entityHandlerRequest->method) { OIC_LOG(INFO, ES_RH_TAG, "Received GET request"); ehRet = ProcessGetRequest(entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OIC_LOG(INFO, ES_RH_TAG, "Received PUT request"); //PUT request will be handled in the internal implementation if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle) { ehRet = ProcessPutRequest(entityHandlerRequest, &payload); } else { OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put"); ehRet = OC_EH_ERROR; } } else if (OC_REST_POST == entityHandlerRequest->method) { OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client"); if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle) { ehRet = ProcessPostRequest(entityHandlerRequest, &payload); } else { OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put"); ehRet = OC_EH_ERROR; } } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; //response uses OCPaylod while all get,put methodes use OCRepPayload response.payload = (OCPayload*) (payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof(response.sendVendorSpecificHeaderOptions)); memset(response.resourceUri, 0, sizeof(response.resourceUri)); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OIC_LOG(ERROR, ES_RH_TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } return ehRet; }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest* entityHandlerRequest, void *callback) { (void) callback; OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = { 0 }; OCRepPayload* payload = NULL; if (entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG(INFO, ES_RH_TAG, "Received GET request"); ehRet = ProcessGetRequest(entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG(INFO, ES_RH_TAG, "Received PUT request"); if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle) { ehRet = ProcessPutRequest(entityHandlerRequest, &payload); } else { ehRet = OC_EH_ERROR; } } else if (OC_REST_POST == entityHandlerRequest->method) { // TODO: As of now, POST request will be not received. OC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client"); //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; //response uses OCPaylod while all get,put methodes use OCRepPayload response.payload = (OCPayload*) (payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, ES_RH_TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (g_flag == 1) { g_cbForResEvent(ES_RECVTRIGGEROFPROVRES); g_flag = 0; } return ehRet; }