Ejemplo n.º 1
0
static int event_handler(struct mg_event *event) {

  if (event->type == MG_REQUEST_BEGIN) {
    if (!strcmp(event->request_info->uri, "/handle_post_request")) {
      char path[200];
      FILE *fp = mg_upload(event->conn, "/tmp", path, sizeof(path));
      if (fp != NULL) {
        fclose(fp);
        mg_printf(event->conn, "HTTP/1.0 200 OK\r\n\r\nSaved: [%s]", path);
      } else {
        mg_printf(event->conn, "%s", "HTTP/1.0 200 OK\r\n\r\nNo files sent");
      }
    } else {
      // Show HTML form. Make sure it has enctype="multipart/form-data" attr.
      static const char *html_form =
        "<html><body>Upload example."
        "<form method=\"POST\" action=\"/handle_post_request\" "
        "  enctype=\"multipart/form-data\">"
        "<input type=\"file\" name=\"file\" /> <br/>"
        "<input type=\"submit\" value=\"Upload\" />"
        "</form></body></html>";

      mg_printf(event->conn, "HTTP/1.0 200 OK\r\n"
          "Content-Length: %d\r\n"
          "Content-Type: text/html\r\n\r\n%s",
          (int) strlen(html_form), html_form);
    }

    // Mark request as processed
    return 1;
  }

  // All other events left unprocessed
  return 1;
}
Ejemplo n.º 2
0
// 2. PUT_FILE
static void proc_cmd_put_file(struct mg_connection* conn, const struct mg_request_info* request_info)
{
	char str_path[200];
	char str_target_dir[200];

	get_qsvar(request_info, "path", str_path, sizeof(str_path));

	if (strcmp(str_path, CONF_DIRECTORY) != 0 && strcmp(str_path, DATA_DIRECTORY) != 0)
	{
		logger_remotem(" put_file path not valid = %s; switch to %s", str_path, UPLOAD_DIRECTORY);
		sprintf(str_path, "%s", UPLOAD_DIRECTORY);
	}

	sprintf(str_target_dir, "%s%s", ODI_HOME, str_path);
	memset(str_path, 0, 200);
	logger_remotem("PUT_FILE: path - %s", str_target_dir);
	FILE* fp = mg_upload(conn, str_target_dir, str_path, sizeof(str_path));
	if (fp != NULL)
	{
		fclose(fp);
		logger_remotem("Upload success: put_file path - %s", str_path);
	}
	else
	{
		logger_remotem("PUT_FILE : Failed to send.");
		mg_printf(conn,
			"HTTP/1.0 200 OK\r\n\r\nPut file failed\r\nerrno=%d\r\n",
			PUT_FILE_FAILED);
		return;
	}

	mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\nerrno=0\r\n");
}
Ejemplo n.º 3
0
/* callback: used to generate all content */
static int begin_request_handler(struct mg_connection *conn)
{
    if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
        mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
        mg_upload(conn, "/tmp");
    } else {
        /* Show HTML form. */
        /* See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 */
        static const char *html_form =
            "<html><body>Upload example."
            ""
            /* enctype="multipart/form-data" */
            "<form method=\"POST\" action=\"/handle_post_request\" "
            "  enctype=\"multipart/form-data\">"
            "<input type=\"file\" name=\"file\" /> <br/>"
            "<input type=\"file\" name=\"file2\" /> <br/>"
            "<input type=\"submit\" value=\"Upload\" />"
            "</form>"
            ""
            "</body></html>";

        mg_printf(conn, "HTTP/1.0 200 OK\r\n"
                  "Content-Length: %d\r\n"
                  "Content-Type: text/html\r\n\r\n%s",
                  (int) strlen(html_form), html_form);
    }

    // Mark request as processed
    return 1;
}
Ejemplo n.º 4
0
    void Request::upload(string targetDirectory)
    {
        char path[1024];

        if (mg_upload(connection, targetDirectory.c_str(), path, sizeof(path)) != NULL) {
            uploadFiles.push_back(string(path));
        }
    }
Ejemplo n.º 5
0
int DomainServer::civetwebRequestHandler(struct mg_connection *connection) {
    const struct mg_request_info* ri = mg_get_request_info(connection);
    
    if (strcmp(ri->uri, "/assignment") == 0 && strcmp(ri->request_method, "POST") == 0) {
        // return a 200
        mg_printf(connection, "%s", "HTTP/1.0 200 OK\r\n\r\n");
        // upload the file
        mg_upload(connection, "/tmp");
        
        return 1;
    } else {
        // have mongoose process this request from the document_root
        return 0;
    }
}
Ejemplo n.º 6
0
static void test_upload(struct mg_connection *conn, const char *orig_path,
                        const char *uploaded_path) {
  int len1, len2;
  char path[500], *p1, *p2;
  FILE *fp;

  ASSERT((fp = mg_upload(conn, ".", path, sizeof(path))) != NULL);
  fclose(fp);
  ASSERT(!strcmp(path, uploaded_path));
  ASSERT((p1 = read_file(orig_path, &len1)) != NULL);
  ASSERT((p2 = read_file(path, &len2)) != NULL);
  ASSERT(len1 == len2);
  ASSERT(memcmp(p1, p2, len1) == 0);
  free(p1), free(p2);
  remove(path);
}
Ejemplo n.º 7
0
static int begin_request_handler_cb(struct mg_connection *conn) {
  const struct mg_request_info *ri = mg_get_request_info(conn);

  if (!strcmp(ri->uri, "/data")) {
    mg_printf(conn, "HTTP/1.1 200 OK\r\n"
              "Content-Length: %d\r\n"
              "Content-Type: text/plain\r\n\r\n"
              "%s", (int) strlen(fetch_data), fetch_data);
    return 1;
  }

  if (!strcmp(ri->uri, "/upload")) {
    ASSERT(mg_upload(conn, ".") == 1);
  }

  return 0;
}
Ejemplo n.º 8
0
static int begin_request_handler_cb(struct mg_connection *conn) {
  const struct mg_request_info *ri = mg_get_request_info(conn);

  if (!strcmp(ri->uri, "/data")) {
    mg_printf(conn, "HTTP/1.1 200 OK\r\n"
              "Content-Type: text/plain\r\n\r\n"
              "%s", fetch_data);
    close_connection(conn);
    return 1;
  }

  if (!strcmp(ri->uri, "/upload")) {
    ASSERT(ri->query_string != NULL);
    ASSERT(mg_upload(conn, ".") == atoi(ri->query_string));
  }

  return 0;
}
Ejemplo n.º 9
0
static int event_handler(struct mg_event *event) {
  struct mg_request_info *ri = event->request_info;

  if (event->type == MG_REQUEST_BEGIN) {
    if (!strcmp(ri->uri, "/data")) {
      mg_printf(event->conn, "HTTP/1.1 200 OK\r\n"
          "Content-Type: text/plain\r\n\r\n"
          "%s", fetch_data);
      close_connection(event->conn);
      return 1;
    }

    if (!strcmp(ri->uri, "/zerolen")) {
      char buf[100];
      mg_printf(event->conn, "%s",
                "HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nok");
      printf("[%d]\n", mg_read(event->conn, buf, sizeof(buf)));
      ASSERT(mg_read(event->conn, buf, sizeof(buf)) == 0);
      return 1;
    }

    if (!strcmp(ri->uri, "/upload")) {
      test_upload(event->conn, "lua_5.2.1.h", "./f1.txt");
      test_upload(event->conn, "lsqlite3.c", "./f2.txt");
      ASSERT(mg_upload(event->conn, ".", NULL, 0) == NULL);

      mg_printf(event->conn, "HTTP/1.0 200 OK\r\n"
                "Content-Type: text/plain\r\n\r\n"
                "%s", upload_ok_message);
      close_connection(event->conn);
      return 1;
    }
  } else if (event->type == MG_EVENT_LOG) {
    printf("%s\n", (const char *) event->event_param);
  }

  return 0;
}
Ejemplo n.º 10
0
static int begin_request_handler(struct mg_connection *conn) {
  if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
    mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
    mg_upload(conn, "./tmp");
  } else {
    // Show HTML form. Make sure it has enctype="multipart/form-data" attr.
    static const char *html_form =
      "<html><body>Upload example."
      "<form method=\"POST\" action=\"/handle_post_request\" "
      "  enctype=\"multipart/form-data\">"
      "<input type=\"file\" name=\"file\" /> <br/>"
      "<input type=\"submit\" value=\"Upload\" />"
      "</form></body></html>";

    mg_printf(conn, "HTTP/1.0 200 OK\r\n"
              "Content-Length: %d\r\n"
              "Content-Type: text/html\r\n\r\n%s",
              (int) strlen(html_form), html_form);
  }

  // Mark request as processed
  return 1;
}
Ejemplo n.º 11
0
int DomainServer::civetwebRequestHandler(struct mg_connection *connection) {
    const struct mg_request_info* ri = mg_get_request_info(connection);
    
    const char RESPONSE_200[] = "HTTP/1.0 200 OK\r\n\r\n";
    const char RESPONSE_400[] = "HTTP/1.0 400 Bad Request\r\n\r\n";
    
    const char URI_ASSIGNMENT[] = "/assignment";
    const char URI_NODE[] = "/node";
    
    if (strcmp(ri->request_method, "GET") == 0) {
        if (strcmp(ri->uri, "/assignments.json") == 0) {
            // user is asking for json list of assignments
            
            // start with a 200 response
            mg_printf(connection, "%s", RESPONSE_200);
            
            // setup the JSON
            QJsonObject assignmentJSON;
            QJsonObject assignedNodesJSON;
            
            // enumerate the NodeList to find the assigned nodes
            NodeList* nodeList = NodeList::getInstance();
            
            for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
                if (node->getLinkedData()) {
                    // add the node using the UUID as the key
                    QString uuidString = uuidStringWithoutCurlyBraces(node->getUUID());
                    assignedNodesJSON[uuidString] = jsonObjectForNode(&(*node));
                }
            }
            
            assignmentJSON["fulfilled"] = assignedNodesJSON;
            
            QJsonObject queuedAssignmentsJSON;
            
            // add the queued but unfilled assignments to the json
            std::deque<Assignment*>::iterator assignment = domainServerInstance->_assignmentQueue.begin();
            
            while (assignment != domainServerInstance->_assignmentQueue.end()) {
                QJsonObject queuedAssignmentJSON;
                
                QString uuidString = uuidStringWithoutCurlyBraces((*assignment)->getUUID());
                queuedAssignmentJSON[JSON_KEY_TYPE] = QString((*assignment)->getTypeName());
                
                // if the assignment has a pool, add it
                if ((*assignment)->hasPool()) {
                    queuedAssignmentJSON[JSON_KEY_POOL] = QString((*assignment)->getPool());
                }
                
                // add this queued assignment to the JSON
                queuedAssignmentsJSON[uuidString] = queuedAssignmentJSON;
                
                // push forward the iterator to check the next assignment
                assignment++;
            }
            
            assignmentJSON["queued"] = queuedAssignmentsJSON;
            
            // print out the created JSON
            QJsonDocument assignmentDocument(assignmentJSON);
            mg_printf(connection, "%s", assignmentDocument.toJson().constData());
            
            // we've processed this request
            return 1;
        } else if (strcmp(ri->uri, "/nodes.json") == 0) {
            // start with a 200 response
            mg_printf(connection, "%s", RESPONSE_200);
            
            // setup the JSON
            QJsonObject rootJSON;
            QJsonObject nodesJSON;
            
            // enumerate the NodeList to find the assigned nodes
            NodeList* nodeList = NodeList::getInstance();
            
            for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
                // add the node using the UUID as the key
                QString uuidString = uuidStringWithoutCurlyBraces(node->getUUID());
                nodesJSON[uuidString] = jsonObjectForNode(&(*node));
            }
            
            rootJSON["nodes"] = nodesJSON;
            
            // print out the created JSON
            QJsonDocument nodesDocument(rootJSON);
            mg_printf(connection, "%s", nodesDocument.toJson().constData());
            
            // we've processed this request
            return 1;
        }
        
        // not processed, pass to document root
        return 0;
    } else if (strcmp(ri->request_method, "POST") == 0) {
        if (strcmp(ri->uri, URI_ASSIGNMENT) == 0) {
            // return a 200
            mg_printf(connection, "%s", RESPONSE_200);
            // upload the file
            mg_upload(connection, "/tmp");
            
            return 1;
        }
        
        return 0;
    } else if (strcmp(ri->request_method, "DELETE") == 0) {
        // this is a DELETE request
        
        // check if it is for an assignment
        if (memcmp(ri->uri, URI_NODE, strlen(URI_NODE)) == 0) {
            // pull the UUID from the url
            QUuid deleteUUID = QUuid(QString(ri->uri + strlen(URI_NODE) + sizeof('/')));
            
            if (!deleteUUID.isNull()) {
                Node *nodeToKill = NodeList::getInstance()->nodeWithUUID(deleteUUID);
                
                if (nodeToKill) {
                    // start with a 200 response
                    mg_printf(connection, "%s", RESPONSE_200);
                    
                    // we have a valid UUID and node - kill the node that has this assignment
                    NodeList::getInstance()->killNode(nodeToKill);
                    
                    // successfully processed request
                    return 1;
                }
            }
        }
        
        // request not processed - bad request
        mg_printf(connection, "%s", RESPONSE_400);
        
        // this was processed by civetweb
        return 1;
    } else {
        // have mongoose process this request from the document_root
        return 0;
    }
}
Ejemplo n.º 12
0
bool FloppyResource::dispatch(mg_connection *conn, mg_request_info *req_info, std::string sResourceInfo /*=""*/ )
{
    Debug::out(LOG_DEBUG, "FloppyResource::dispatch");

    if( strstr(req_info->request_method,"GET")==0 && strstr(req_info->request_method,"POST")==0 && strstr(req_info->request_method,"PUT")==0 ){
        mg_printf(conn, "HTTP/1.1 405 Method Not Allowed %s\r\n",req_info->request_method);
        return true;
    }

    //return slot info
    if( strstr(req_info->request_method,"GET")>0 ){
        mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n");
        mg_printf(conn, "Cache: no-cache\r\n");
        std::ostringstream stringStream;
        stringStream << "{\"slots\":[";
        stringStream << "\"" << pxFloppyService->getImageName(0) << "\",";
        stringStream << "\"" << pxFloppyService->getImageName(1) << "\",";
        stringStream << "\"" << pxFloppyService->getImageName(2) << "\"";
        stringStream << "],";
        if( pxFloppyService->getActiveSlot()==EMPTY_IMAGE_SLOT ){
          stringStream << "\"active\": null";
        }else{
          stringStream << "\"active\":" << pxFloppyService->getActiveSlot();
        }
        stringStream << ","; 
        stringStream << "\"encoding_ready\":";

        /*
        if( pxFloppyService->isImageReady() ){
          stringStream << "true";
        }else{
          stringStream << "false";
        }*/
        stringStream << "true";     // always ready now (encoding not blocking usage of image)

        stringStream << "}";
        std::string sJson=stringStream.str();
        mg_printf(conn, "Content-Length: %lu\r\n\r\n",(unsigned long)sJson.length());   // Always set Content-Length
        mg_write(conn, sJson.c_str(), sJson.length());                  // send content
        return true;
    }

    //upload image to slot
    if( strstr(req_info->request_method,"POST")>0 ){
        Debug::out(LOG_DEBUG, "/floppy POST");

        int iSlot=atoi(sResourceInfo.c_str());        
        if( iSlot<0 || iSlot>2 )
        {
            mg_printf(conn, "HTTP/1.1 400 Selected slot not in range 0 to 2\r\n");
            return true;
        }

        int iFiles=mg_upload(conn, "/tmp");

        if( iFiles!=1 )
        {
            mg_printf(conn, "HTTP/1.1 400 No file data\r\n");
            return true;
        }

        pxFloppyService->setImage(iSlot,WebServer::sLastUploadedFile);

        mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
        mg_printf(conn, "<html><body>\n"); 
        mg_printf(conn, "Slot %d\n",iSlot); 
        mg_printf(conn, "File %d %s\n",iFiles,WebServer::sLastUploadedFile.c_str()); 
        mg_printf(conn, "</body></html>\n"); 
        return true;    
    }

    //set current slot
    if( strstr(req_info->request_method,"PUT")>0 ){
        int iSlot=atoi(sResourceInfo.c_str());
        
        if(iSlot == 100) {      // special case -- slot 100 == INSERT CONFIG IMAGE
            events.insertSpecialFloppyImageId = SPECIAL_FDD_IMAGE_CE_CONF;

            mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
            return true;
        }

        if(iSlot == 101) {      // special case -- slot 101 == INSERT FLOPPY TEST IMAGE
            events.insertSpecialFloppyImageId = SPECIAL_FDD_IMAGE_FDD_TEST;

            mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
            return true;
        }
        
        //slot 3=deactivated        
        if( iSlot<0 || iSlot>3 )
        {
            mg_printf(conn, "HTTP/1.1 400 Selected slot not in range 0 to 3\r\n");
            return true;
        }
        
        pxFloppyService->setActiveSlot(iSlot);
        mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
        return true;
    }

    return false;
}