~ubuntu-branches/ubuntu/trusty/nordugrid-arc/trusty

« 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: 2013-11-29 13:39:10 UTC
  • mfrom: (3.1.16 sid)
  • Revision ID: package-import@ubuntu.com-20131129133910-sy6ayoavphc5hozs
Tags: 4.0.0-1
4.0.0 Release (Closes: #715131) (LP: #1049798)

Show diffs side-by-side

added added

removed removed

Lines of Context:
282
282
    // TODO: check if it is last tag
283
283
    multipart_ = MULTIPART_END;
284
284
  };
 
285
  logger.msg(Arc::DEBUG,"<< %s",std::string(buf,size));
285
286
  return true;
286
287
}
287
288
 
288
289
bool PayloadHTTPIn::flush_multipart(void) {
 
290
  // TODO: protect against insame length of body
289
291
  if(!multipart_) return true;
290
292
  if(multipart_ == MULTIPART_ERROR) return false;
291
293
  std::string::size_type pos = 0;
496
498
  int64_t result_size = 0;
497
499
  if(length_ == 0) {
498
500
    valid_=true;
 
501
    body_read_=true;
499
502
    return true;
500
503
  } else if(length_ > 0) {
501
504
    // TODO: combination of chunked and defined length is probably impossible
 
505
    // TODO: protect against insane length_
502
506
    result=(char*)malloc(length_+1);
503
507
    if(!read_multipart(result,length_)) { free(result); return false; };
504
508
    result_size=length_;
511
515
      result=new_result;
512
516
      if(!read_multipart(result+result_size,chunk_size)) break;
513
517
      // TODO: logical size is not always same as end of body
 
518
      // TODO: protect against insane length of body
514
519
      result_size+=chunk_size;
515
520
    };
516
521
  };
526
531
  // allign to end of message
527
532
  flush_multipart();
528
533
  flush_chunked();
 
534
  body_read_=true;
529
535
  return true;
530
536
}
531
537
 
532
538
PayloadHTTPIn::PayloadHTTPIn(PayloadStreamInterface& stream,bool own):
533
539
    chunked_(CHUNKED_NONE),chunk_size_(0),
534
540
    multipart_(MULTIPART_NONE),stream_(&stream),stream_offset_(0),
535
 
    stream_own_(own),fetched_(false),body_(NULL),body_size_(0) {
 
541
    stream_own_(own),fetched_(false),header_read_(false),body_read_(false),
 
542
    body_(NULL),body_size_(0) {
536
543
  tbuf_[0]=0; tbuflen_=0;
537
544
  if(!parse_header()) {
538
545
    error_ = IString("Failed to parse HTTP header").str();
539
546
    return;
540
547
  }
 
548
  header_read_=true;
541
549
  valid_=true;
542
550
}
543
551
 
544
552
PayloadHTTPIn::~PayloadHTTPIn(void) {
545
 
  // allign to end of message
 
553
  // allign to end of message (maybe not needed with Sync() exposed)
546
554
  flush_multipart();
547
555
  flush_chunked();
548
556
  if(stream_ && stream_own_) delete stream_;
641
649
  if(length_ == 0) {
642
650
    // No body
643
651
    size=0;
 
652
    body_read_=true;
644
653
    return false;
645
654
  };
646
655
  if(length_ > 0) {
653
662
      size=bs; return false;
654
663
    };
655
664
    size=bs; stream_offset_+=bs;
 
665
    if(stream_offset_ >= length_) body_read_=true;
656
666
    return true;
657
667
  };
658
668
  // Ordinary stream with no length known
659
669
  int64_t tsize = size;
660
670
  bool r = read_multipart(buf,tsize);
661
671
  if(r) stream_offset_+=tsize;
 
672
  if(!r) body_read_=true;
662
673
  size=tsize;
663
674
  // TODO: adjust logical parameters of buffers
664
675
  return r;
689
700
  return (offset_ + body_size_);
690
701
}
691
702
 
 
703
bool PayloadHTTPIn::Sync(void) {
 
704
  if(!valid_) return false;
 
705
  if(!header_read_) return false;
 
706
  if(fetched_) return true;
 
707
  // For multipart data - read till end tag
 
708
  // If data is chunked then it is enough to just read till 
 
709
  // chunks are over.
 
710
  if(multipart_ || chunked_) {
 
711
    bool r = true;
 
712
    if(!flush_multipart()) r=false; // not really neaded but keeps variables ok
 
713
    if(!flush_chunked()) r=false;
 
714
    if(r) body_read_ = true;
 
715
    return r;
 
716
  };
 
717
  // For data without any tags just read till end reached
 
718
  for(;!body_read_;) {
 
719
    char buf[1024];
 
720
    int size = sizeof(buf);
 
721
    bool r = Get(buf,size);
 
722
    if(body_read_) return true;
 
723
    if(!r) break;
 
724
  };
 
725
  return false;
 
726
}
692
727
 
693
728
// ------------------- PayloadHTTPOut ---------------------------
694
729