1
0

httpClient.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef HTTPCLIENT_H
  2. #define HTTPCLIENT_H
  3. #include <string>
  4. #include <vector>
  5. #include <boost/noncopyable.hpp>
  6. #include <boost/shared_ptr.hpp>
  7. #include <boost/thread.hpp>
  8. #include <openssl/crypto.h>
  9. #include <curl/curl.h>
  10. static std::vector<boost::shared_ptr<boost::mutex> > g_mutexes;
  11. static void lock_callback(int mode, int type, const char *file, int line)
  12. {
  13. if (mode & CRYPTO_LOCK)
  14. g_mutexes[type]->lock();
  15. else
  16. g_mutexes[type]->unlock();
  17. }
  18. static unsigned long thread_id(void)
  19. {
  20. return (unsigned long)pthread_self();
  21. }
  22. static void initialize_locks(void)
  23. {
  24. g_mutexes.resize(CRYPTO_num_locks());
  25. for (int i = 0; i < CRYPTO_num_locks(); ++i)
  26. g_mutexes[i] = boost::shared_ptr<boost::mutex>(new boost::mutex());
  27. CRYPTO_set_id_callback(thread_id);
  28. CRYPTO_set_locking_callback(lock_callback);
  29. }
  30. static void free_locks(void)
  31. {
  32. CRYPTO_set_locking_callback(NULL);
  33. }
  34. class httpClient : boost::noncopyable
  35. {
  36. public:
  37. httpClient();
  38. ~httpClient();
  39. std::string GetUrlContents(const std::string& url) const;
  40. private:
  41. static size_t WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData);
  42. private:
  43. std::vector<std::string> m_userAgents;
  44. };
  45. #endif // HTTPCLIENT_H