HttpConnection.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_HTTP_CONNECTION_H_
  23. #define _D_HTTP_CONNECTION_H_
  24. #include "Segment.h"
  25. #include "Socket.h"
  26. #include "Request.h"
  27. #include "Option.h"
  28. #include "Logger.h"
  29. #include "HttpHeader.h"
  30. #include "common.h"
  31. #include "Logger.h"
  32. #include <string>
  33. using namespace std;
  34. #define HEADERBUF_SIZE 4096
  35. class HttpConnection {
  36. private:
  37. string getHost(const string& host, int port) const;
  38. string createRequest(const Segment& segment) const;
  39. int findEndOfHeader(const char* buf, const char* substr, int bufLength) const;
  40. bool useProxy() const;
  41. bool useProxyAuth() const;
  42. bool useProxyGet() const;
  43. string getProxyAuthString() const;
  44. int cuid;
  45. SocketHandle socket;
  46. const Request* req;
  47. const Option* option;
  48. const Logger* logger;
  49. char headerBuf[HEADERBUF_SIZE+1];
  50. int headerBufLength;
  51. public:
  52. HttpConnection(int cuid, const SocketHandle& socket, const Request* req,
  53. const Option* op);
  54. /**
  55. * Sends Http request.
  56. * If segment.sp+segment.ds > 0 then Range header is added.
  57. * This method is used in HTTP/HTTP downloading and FTP downloading via
  58. * HTTP proxy(GET method).
  59. * @param segment indicates starting postion of the file for downloading
  60. */
  61. void sendRequest(const Segment& segment) const;
  62. /**
  63. * Sends Http proxy request using CONNECT method.
  64. */
  65. void sendProxyRequest() const;
  66. /**
  67. * Receives HTTP response from the server and store the response header
  68. * into the variable headers.
  69. * If response header is not fully received, received header is buffured
  70. * in this object and headers is undefined and this method returns 0.
  71. * You should continue to call this method until whole response header is
  72. * received and this method returns non-zero value.
  73. *
  74. * @param headers holder to store HTTP response header
  75. * @return HTTP status or 0 if whole response header is not received
  76. */
  77. int receiveResponse(HttpHeader& headers);
  78. };
  79. #endif // _D_HTTP_CONNECTION_H_