~ubuntu-branches/debian/sid/nordugrid-arc/sid

« back to all changes in this revision

Viewing changes to src/hed/mcc/http/PayloadHTTP.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2012-12-13 16:41:31 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20121213164131-0fumka0jar8mxm07
Tags: 2.0.1-1
* 2.0.1 Release
* Drop patches accepted upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
}
68
68
 
69
69
bool PayloadHTTP::read(char* buf,int64_t& size) {
70
 
char* ss = buf;
71
70
  if(tbuflen_ >= size) {
72
71
    memcpy(buf,tbuf_,size);
73
72
    memmove(tbuf_,tbuf_+size,tbuflen_-size+1);
428
427
    std::string line;
429
428
    for(;;) {
430
429
      if(!readline_chunked(line)) return false;
431
 
      if(line.length() >= multipart_tag_.length()) {
 
430
      if(line.length() == multipart_tag_.length()) {
432
431
        if(strncmp(line.c_str(),multipart_tag_.c_str(),multipart_tag_.length()) == 0) {
433
432
          multipart_ = MULTIPART_BODY;
434
433
          break;
515
514
    multipart_(MULTIPART_NONE),keep_alive_(true),stream_offset_(0),
516
515
    head_response_(false) {
517
516
  tbuf_[0]=0; tbuflen_=0;
518
 
  if(!parse_header()) return;
 
517
  if(!parse_header()) {
 
518
    error_ = IString("Failed to parse HTTP header").str();
 
519
    return;
 
520
  }
519
521
  // If stream_ is owned then body can be fetched later
520
522
  // if(!stream_own_) if(!get_body()) return;
521
523
  valid_=true;
577
579
bool PayloadHTTP::Flush(void) {
578
580
  std::string header;
579
581
  bool to_stream = (stream_ != NULL);
580
 
  if(method_.empty() && (code_ == 0)) return false;
 
582
  if(method_.empty() && (code_ == 0)) {
 
583
    error_ = IString("Invalid HTTP object can't produce result").str();
 
584
    return false;
 
585
  };
581
586
  // Computing length of Body part
582
587
  int64_t length = 0;
583
588
  std::string range_header;
667
672
  header+="\r\n";
668
673
  logger.msg(Arc::DEBUG,"> %s",header);
669
674
  if(to_stream) {
670
 
    if(!stream_->Put(header)) return false;
 
675
    if(!stream_->Put(header)) {
 
676
      error_ = IString("Failed to write header to output stream").str();
 
677
      return false;
 
678
    };
671
679
    if(length > 0) {
672
680
      if(sbody_) {
673
681
        // stream to stream transfer
675
683
        // TODO: parallel read and write for better performance
676
684
        int tbufsize = (length>1024*1024)?(1024*1024):length;
677
685
        char* tbuf = new char[tbufsize];
678
 
        if(!tbuf) return false;
 
686
        if(!tbuf) {
 
687
          error_ = IString("Memory allocation error").str();
 
688
          return false;
 
689
        };
679
690
        for(;;) {
680
691
          int lbuf = tbufsize;
681
692
          if(!sbody_->Get(tbuf,lbuf)) break;
682
 
          if(!stream_->Put(tbuf,lbuf)) { delete[] tbuf; return false; };
 
693
          if(!stream_->Put(tbuf,lbuf)) {
 
694
            error_ = IString("Failed to write body to output stream").str();
 
695
            delete[] tbuf;
 
696
            return false;
 
697
          };
683
698
        };
684
699
        delete[] tbuf;
685
700
      } else {
687
702
          char* tbuf = Buffer(n);
688
703
          if(tbuf == NULL) break;
689
704
          int64_t lbuf = BufferSize(n);
690
 
          if(lbuf > 0) if(!stream_->Put(tbuf,lbuf)) return false;
 
705
          if(lbuf > 0) if(!stream_->Put(tbuf,lbuf)) {
 
706
            error_ = IString("Failed to write body to output stream").str();
 
707
            return false;
 
708
          };
691
709
        };
692
710
      };
693
711
    };
836
854
}
837
855
 
838
856
bool PayloadHTTP::Get(char* buf,int& size) {
 
857
  if(!valid_) return false;
839
858
  if(fetched_) {
840
859
    // Read from buffers
841
860
    uint64_t bo = 0;