// Send a file // Takes a file name as an argument. Since we cannot send arbitary // sized data on an underlying UDP transport, we are going to break // the file into chunk sizes. By default we will use a chunk size of // 256 but the user can set a different size // // Returns -1 on error and bytes sent on success size_t FileTransferClient::SendFile (const string &filename) { ACE_Mem_Map mmap; // we choose to use memory mapped files so that we // can access the file contents directly from a // buffer // now map the file into memory if (mmap.map (filename.c_str ()) == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("[%P] FileTransferClientClient::SendFile - ") ACE_TEXT ("failed to map file (%m)\n"))); return -1; } // delegate sending of the the file to our reliable protocol handler return this->proto_object_->send_data (mmap.addr (), mmap.size ()); }
int ACE_TMAIN (int argc, ACE_TCHAR **argv) { ACE_LOG_MSG->open (argv[0]); if (argc != 2) ACE_ERROR_RETURN ((LM_ERROR, "usage: %n file\n"), -1); ACE_Mem_Map mmap; if (mmap.map (argv[1], static_cast<size_t> (-1), O_RDWR) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%n: %p\n", "mmap"), -1); print_array_in_reverse ((char *) mmap.addr (), ACE_Utils::truncate_cast<int> (mmap.size ())); return 0; }
int main (int argc, char **argv) { ACE_LOG_MSG->open (argv[0]); if (argc != 2) ACE_ERROR_RETURN ((LM_ERROR, "usage: %n file\n"), -1); ACE_Mem_Map mmap; if (mmap.map (argv[1], -1, O_RDWR) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%n: %p\n", "mmap"), -1); print_array_in_reverse ((char *) mmap.addr (), mmap.size ()); return 0; }
void HTTP_Response::normal_response (void) { const char *hv = 0;; ACE_DEBUG ((LM_DEBUG, " (%t) %s request for %s [%s], version %s\n", request_.method (), request_.uri (), request_.path (), (request_.version () ? request_.version () : "HTTP/0.9"))); switch (this->request_.type ()) { case HTTP_Request::GET : this->build_headers (); this->io_.transmit_file (this->request_.path (), this->HTTP_HEADER, this->HTTP_HEADER_LENGTH, this->HTTP_TRAILER, this->HTTP_TRAILER_LENGTH); break; case HTTP_Request::HEAD : this->build_headers (); this->io_.send_confirmation_message (this->HTTP_HEADER, this->HTTP_HEADER_LENGTH); break; case HTTP_Request::POST : // What to do here? // Standard says this is implementation dependent. // Examples: annotations, page updates, etc. // This may be a good place to stick CORBA stuff, // and mobile code. this->error_response (HTTP_Status_Code::STATUS_NOT_IMPLEMENTED, "Requested method is not implemented."); break; case HTTP_Request::PUT : // Only commit to this if we can authenticate it // if there is no Authentication: header on the incoming request, // deny it hv = this->request_.headers ()["Authorization"].value (); if (hv == 0 || *hv == '\0') this->error_response (HTTP_Status_Code::STATUS_UNAUTHORIZED, "Unauthorized to use PUT method"); else if (ACE_OS::strncmp (hv, "Basic ", 6) != 0) // ``6'' is the length of the string "Basic " this->error_response (HTTP_Status_Code::STATUS_UNAUTHORIZED, "Unknown authorization method"); else { ACE_Mem_Map mmapfile; const char *hvv = hv + 6; // Skip past the string "Basic " char *buf = new char [ACE_OS::strlen (hv)]; char *auth = HTTP_Helper::HTTP_decode_base64 (ACE_OS::strcpy (buf, hvv)); if (mmapfile.map (ACE_TEXT ("jaws.auth")) != -1 && auth != 0 && ACE_OS::strstr((const char *) mmapfile.addr (), auth) != 0) this->io_.receive_file (this->request_.path (), this->request_.data (), this->request_.data_length (), this->request_.content_length ()); else this->error_response (HTTP_Status_Code::STATUS_UNAUTHORIZED, "Invalid authorization attempt"); delete [] buf; } break; default : this->error_response (HTTP_Status_Code::STATUS_NOT_IMPLEMENTED, "Requested method is not implemented."); } }
int run_main (int, ACE_TCHAR *[]) { ACE_START_TEST (ACE_TEXT ("Mem_Map_Test")); #if !defined (ACE_LACKS_MMAP) // = Initialize the temporary variable names ACE_TCHAR test_file[MAXPATHLEN + 1]; ACE_TCHAR temp_file1[MAXPATHLEN + 1]; ACE_TCHAR temp_file2[MAXPATHLEN + 1]; // Get the temporary directory // - 18 is for the filenames, ace_mem_map_temp_1 is the longest if (ACE::get_temp_dir (test_file, MAXPATHLEN - 18) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Temporary path too long\n")), -1); // Copy the temp directory to the other variables ACE_OS::strcpy (temp_file1, test_file); ACE_OS::strcpy (temp_file2, test_file); // Add the filenames to the end ACE_OS::strcat (test_file, ACE_TEXT ("ace_mem_map_test")); ACE_OS::strcat (temp_file1, ACE_TEXT ("ace_mem_map_temp_1")); ACE_OS::strcat (temp_file2, ACE_TEXT ("ace_mem_map_temp_2")); // First create a test file to work on if (create_test_file (test_file, LINE_LENGTH, NUM_LINES) != 0) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Create test file failed\n")), -1); ACE_Mem_Map mmap; // First memory map the test file if (mmap.map (test_file) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%n: %p\n%a"), ACE_TEXT ("mmap")), -1); // Now create a temporary file for intermediate processing ACE_HANDLE temp_file_handle = ACE_OS::open (temp_file1, O_RDWR | O_TRUNC | O_CREAT, ACE_DEFAULT_FILE_PERMS); if (temp_file_handle == ACE_INVALID_HANDLE) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed\n")), -1); // Reverse the original file and write the output to the temporary // file. reverse_file (temp_file_handle, (char *) mmap.addr (), mmap.size ()); ACE_OS::close (temp_file_handle); ACE_Mem_Map temp_mmap; // Now memory map the temporary file if (temp_mmap.map (temp_file1) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%n: %p\n%a"), ACE_TEXT ("mmap")), -1); if ((temp_file_handle = ACE_OS::open (temp_file2, O_RDWR | O_TRUNC | O_CREAT, ACE_DEFAULT_FILE_PERMS)) == ACE_INVALID_HANDLE) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed\n")), -1); // Now reverse the temporary file and write everything to the second // temporary file. reverse_file (temp_file_handle, (char *) temp_mmap.addr (), temp_mmap.size ()); ACE_OS::close (temp_file_handle); // Memory map the second temporary file ACE_Mem_Map temp_mmap2; if (temp_mmap2.map (temp_file2) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%n: %p\n%a"), ACE_TEXT ("mmap")), -1); // Now do a memcmp -- the orig file and the second temporary file // should be identical. ACE_ASSERT (ACE_OS::memcmp (temp_mmap2.addr (), mmap.addr (), mmap.size ()) == 0); // Delete the test file mmap.remove (); // Delete ACE_TEMP_TEST_FILE temp_mmap.remove (); // Delete ACE_TEMP_TEST_FILE_2 temp_mmap2.remove (); #else /* !ACE_LACKS_MMAP */ ACE_ERROR ((LM_INFO, ACE_TEXT ("mmap is not supported on this platform\n"))); #endif /* !ACE_LACKS_MMAP */ ACE_END_TEST; return 0; }