mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-20 05:37:57 +00:00
Transfer should work for small files that arrive with the headers
This commit is contained in:
parent
5e915bcc09
commit
0255d7938e
114
rhizome_fetch.c
114
rhizome_fetch.c
@ -752,40 +752,7 @@ void rhizome_fetch_write(rhizome_file_fetch_record *q){
|
||||
}
|
||||
}
|
||||
|
||||
void rhizome_fetch_poll(struct sched_ent *alarm)
|
||||
{
|
||||
rhizome_file_fetch_record *q=(rhizome_file_fetch_record *)alarm;
|
||||
|
||||
if (alarm->poll.revents==0){
|
||||
// timeout, close the socket
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(q->state)
|
||||
{
|
||||
case RHIZOME_FETCH_CONNECTING:
|
||||
case RHIZOME_FETCH_SENDINGHTTPREQUEST:
|
||||
rhizome_fetch_write(q);
|
||||
break;
|
||||
case RHIZOME_FETCH_RXFILE:
|
||||
/* Keep reading until we have the promised amount of data */
|
||||
|
||||
sigPipeFlag=0;
|
||||
|
||||
errno=0;
|
||||
char buffer[8192];
|
||||
|
||||
int bytes=read(q->alarm.poll.fd,buffer,8192);
|
||||
|
||||
/* If we got some data, see if we have found the end of the HTTP request */
|
||||
if (bytes>0) {
|
||||
|
||||
// reset timeout
|
||||
unschedule(&q->alarm);
|
||||
q->alarm.alarm=overlay_gettime_ms() + RHIZOME_IDLE_TIMEOUT;
|
||||
q->alarm.deadline = q->alarm.alarm+RHIZOME_IDLE_TIMEOUT;
|
||||
schedule(&q->alarm);
|
||||
void rhizome_write_content(rhizome_file_fetch_record *q, char *buffer, int bytes){
|
||||
|
||||
if (bytes>(q->file_len-q->file_ofs))
|
||||
bytes=q->file_len-q->file_ofs;
|
||||
@ -798,12 +765,6 @@ void rhizome_fetch_poll(struct sched_ent *alarm)
|
||||
}
|
||||
q->file_ofs+=bytes;
|
||||
|
||||
} else if (bytes==0) {
|
||||
if (debug & DEBUG_RHIZOME_RX)
|
||||
DEBUG("Got zero bytes, assume socket dead.");
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
if (q->file_ofs>=q->file_len)
|
||||
{
|
||||
/* got all of file */
|
||||
@ -843,6 +804,44 @@ void rhizome_fetch_poll(struct sched_ent *alarm)
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
|
||||
// reset timeout due to activity
|
||||
unschedule(&q->alarm);
|
||||
q->alarm.alarm=overlay_gettime_ms() + RHIZOME_IDLE_TIMEOUT;
|
||||
q->alarm.deadline = q->alarm.alarm+RHIZOME_IDLE_TIMEOUT;
|
||||
schedule(&q->alarm);
|
||||
}
|
||||
|
||||
void rhizome_fetch_poll(struct sched_ent *alarm)
|
||||
{
|
||||
rhizome_file_fetch_record *q=(rhizome_file_fetch_record *)alarm;
|
||||
|
||||
if (alarm->poll.revents==0){
|
||||
// timeout, close the socket
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(q->state)
|
||||
{
|
||||
case RHIZOME_FETCH_CONNECTING:
|
||||
case RHIZOME_FETCH_SENDINGHTTPREQUEST:
|
||||
rhizome_fetch_write(q);
|
||||
break;
|
||||
case RHIZOME_FETCH_RXFILE:
|
||||
/* Keep reading until we have the promised amount of data */
|
||||
|
||||
sigPipeFlag=0;
|
||||
|
||||
errno=0;
|
||||
char buffer[8192];
|
||||
|
||||
int bytes=read(q->alarm.poll.fd,buffer,8192);
|
||||
|
||||
/* If we got some data, see if we have found the end of the HTTP request */
|
||||
if (bytes>0)
|
||||
rhizome_write_content(q, buffer, bytes);
|
||||
|
||||
break;
|
||||
case RHIZOME_FETCH_RXHTTPHEADERS:
|
||||
/* Keep reading until we have two CR/LFs in a row */
|
||||
@ -852,6 +851,14 @@ void rhizome_fetch_poll(struct sched_ent *alarm)
|
||||
bytes=read(q->alarm.poll.fd,&q->request[q->request_len],
|
||||
1024-q->request_len-1);
|
||||
|
||||
if (sigPipeFlag||((bytes==0)&&(errno==0))) {
|
||||
/* broken pipe, so close connection */
|
||||
if (debug&DEBUG_RHIZOME)
|
||||
DEBUG("Closing rhizome fetch connection due to sigpipe");
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
|
||||
/* If we got some data, see if we have found the end of the HTTP request */
|
||||
if (bytes>0) {
|
||||
int lfcount=0;
|
||||
@ -924,31 +931,16 @@ void rhizome_fetch_poll(struct sched_ent *alarm)
|
||||
File is already open, so just write out any initial bytes of the
|
||||
file we read, and update state flag.
|
||||
*/
|
||||
int fileRxBytes=q->request_len-(i+1);
|
||||
if (fileRxBytes>0)
|
||||
if (fwrite(&q->request[i+1],fileRxBytes,1,q->file)!=1)
|
||||
{
|
||||
if (debug&DEBUG_RHIZOME)
|
||||
DEBUGF("Failed writing initial %d bytes to file.",
|
||||
fileRxBytes);
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
q->file_ofs=fileRxBytes;
|
||||
DEBUGF("Transferred (%lld of %lld)",
|
||||
q->file_ofs,q->file_len);
|
||||
|
||||
q->state=RHIZOME_FETCH_RXFILE;
|
||||
}
|
||||
int fileRxBytes=q->request_len-(i+1);
|
||||
|
||||
if (fileRxBytes>0)
|
||||
rhizome_write_content(q, &q->request[i+1], fileRxBytes);
|
||||
|
||||
}
|
||||
|
||||
if (sigPipeFlag||((bytes==0)&&(errno==0))) {
|
||||
/* broken pipe, so close connection */
|
||||
if (debug&DEBUG_RHIZOME)
|
||||
DEBUG("Closing rhizome fetch connection due to sigpipe");
|
||||
rhizome_fetch_close(q);
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
if (debug&DEBUG_RHIZOME)
|
||||
|
Loading…
Reference in New Issue
Block a user