Implement file download.

This commit is contained in:
Michael Jumper 2013-11-12 11:46:32 -08:00
parent b262440ba5
commit d30ade6bef
5 changed files with 114 additions and 16 deletions

View File

@ -308,6 +308,7 @@ BOOL rdp_freerdp_post_connect(freerdp* instance) {
client->file_handler = rdp_guac_client_file_handler; client->file_handler = rdp_guac_client_file_handler;
client->blob_handler = rdp_guac_client_blob_handler; client->blob_handler = rdp_guac_client_blob_handler;
client->end_handler = rdp_guac_client_end_handler; client->end_handler = rdp_guac_client_end_handler;
client->ack_handler = rdp_guac_client_ack_handler;
return TRUE; return TRUE;

View File

@ -207,6 +207,23 @@ typedef struct rdp_freerdp_context {
} rdp_freerdp_context; } rdp_freerdp_context;
/**
* The transfer status of a file being downloaded.
*/
typedef struct guac_rdp_download_status {
/**
* The file ID of the file being downloaded.
*/
int file_id;
/**
* The current position within the file.
*/
uint64_t offset;
} guac_rdp_download_status;
/** /**
* Given the coordinates and dimensions of a rectangle, clips the rectangle to be * Given the coordinates and dimensions of a rectangle, clips the rectangle to be
* within the clipping bounds of the client data, if clipping is active. * within the clipping bounds of the client data, if clipping is active.

View File

@ -639,3 +639,60 @@ int rdp_guac_client_end_handler(guac_client* client, guac_stream* stream) {
return 0; return 0;
} }
int rdp_guac_client_ack_handler(guac_client* client, guac_stream* stream,
char* message, guac_protocol_status status) {
/* Get status */
guac_rdp_download_status* download =
(guac_rdp_download_status*) stream->data;
/* Get filesystem, return error if no filesystem */
guac_rdp_fs* fs = ((rdp_guac_client_data*) client->data)->filesystem;
if (fs == NULL) {
guac_protocol_send_ack(client->socket, stream, "FAIL (NO FS)",
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
guac_socket_flush(client->socket);
return 0;
}
/* If successful, read data */
if (status == GUAC_PROTOCOL_STATUS_SUCCESS) {
/* Attempt read into buffer */
char buffer[4096];
int bytes_read = guac_rdp_fs_read(fs, download->file_id,
download->offset, buffer, sizeof(buffer));
/* If bytes read, send as blob */
if (bytes_read > 0) {
download->offset += bytes_read;
guac_protocol_send_blob(client->socket, stream,
buffer, bytes_read);
}
/* If EOF, send end */
else if (bytes_read == 0) {
guac_protocol_send_end(client->socket, stream);
guac_client_free_stream(client, stream);
}
/* Otherwise, fail stream */
else {
guac_client_log_error(client, "Error reading file for download");
guac_protocol_send_end(client->socket, stream);
guac_client_free_stream(client, stream);
}
guac_socket_flush(client->socket);
}
/* Otherwise, return stream to client */
else
guac_client_free_stream(client, stream);
return 0;
}

View File

@ -73,5 +73,8 @@ int rdp_guac_client_blob_handler(guac_client* client, guac_stream* stream,
int rdp_guac_client_end_handler(guac_client* client, guac_stream* stream); int rdp_guac_client_end_handler(guac_client* client, guac_stream* stream);
int rdp_guac_client_ack_handler(guac_client* client, guac_stream* stream,
char* message, guac_protocol_status status);
#endif #endif

View File

@ -226,11 +226,23 @@ void guac_rdpdr_fs_process_close(guac_rdpdr_device* device,
int i; int i;
char c; char c;
/* Get client */ /* Get client and stream */
guac_client* client = device->rdpdr->client; guac_client* client = device->rdpdr->client;
/* Allocate stream */ int file_id = guac_rdp_fs_open((guac_rdp_fs*) device->data,
file->absolute_path, ACCESS_FILE_READ_DATA, 0,
DISP_FILE_OPEN, 0);
/* If file opened successfully, start stream */
if (file_id >= 0) {
guac_rdp_download_status* status;
/* Associate stream with transfer status */
guac_stream* stream = guac_client_alloc_stream(client); guac_stream* stream = guac_client_alloc_stream(client);
stream->data = status = malloc(sizeof(guac_rdp_download_status));
status->file_id = file_id;
status->offset = 0;
/* Get basename from absolute path */ /* Get basename from absolute path */
char* basename = file->absolute_path; char* basename = file->absolute_path;
@ -244,12 +256,20 @@ void guac_rdpdr_fs_process_close(guac_rdpdr_device* device,
} while (c != '\0'); } while (c != '\0');
GUAC_RDP_DEBUG(2, "Initiating download of \"%s\"", file->absolute_path); GUAC_RDP_DEBUG(2, "Initiating download of \"%s\"",
file->absolute_path);
/* Begin stream */
guac_protocol_send_file(client->socket, stream, guac_protocol_send_file(client->socket, stream,
"application/octet-stream", basename); "application/octet-stream", basename);
guac_socket_flush(client->socket); guac_socket_flush(client->socket);
} }
else
guac_client_log_error(client, "Unable to download \"%s\"",
file->absolute_path);
} /* end if download */
/* Close file */ /* Close file */
guac_rdp_fs_close((guac_rdp_fs*) device->data, file_id); guac_rdp_fs_close((guac_rdp_fs*) device->data, file_id);