You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/11/19 09:33:47 UTC

[GitHub] [pulsar] BewareMyPower commented on issue #12888: CPP SDK did not respond to request 307 during lookup

BewareMyPower commented on issue #12888:
URL: https://github.com/apache/pulsar/issues/12888#issuecomment-973906020


   It looks like the C++ client has configured the URL redirection, see https://github.com/apache/pulsar/blob/b807200552117c299e3776e33537d082a00290d5/pulsar-client-cpp/lib/HTTPLookupService.cc#L182-L184
   
   And it's weird that you log is 
   
   ```
   Error Code 22
   ```
   
   See https://curl.se/libcurl/c/libcurl-errors.html
   
   > CURLE_HTTP_RETURNED_ERROR (22)
   >
   > This is returned if CURLOPT_FAILONERROR is set TRUE and the HTTP server returns an error code that is >= 400.
   
   Could you try following C++ code and see what would happen?
   
   ```c++
   #include <curl/curl.h>
   
   #include <iostream>
   #include <string>
   
   static size_t curlWriteCallback(void *contents, size_t size, size_t nmemb,
                                   void *responseDataPtr) {
     ((std::string *)responseDataPtr)->append((char *)contents, size * nmemb);
     return size * nmemb;
   }
   
   int main() {
     std::string completeUrl =
         "http://1.62.3.1:1001/lookup/v2/topic/persistent/pulsar-q4822m9k7ej8/test-ns/test-topic-1-partition-30";
     std::string header =
         "Authorization: Bearer eyJrZXlJZCI6InB1bHNhci1xcccccxxxxx";
   
     CURL *handle = curl_easy_init();
     if (!handle) {
       return 1;
     }
   
     // set URL
     curl_easy_setopt(handle, CURLOPT_URL, completeUrl.c_str());
   
     // Write callback
     std::string responseData;
     curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, curlWriteCallback);
     curl_easy_setopt(handle, CURLOPT_WRITEDATA, &responseData);
   
     // New connection is made for each call
     curl_easy_setopt(handle, CURLOPT_FRESH_CONNECT, 1L);
     curl_easy_setopt(handle, CURLOPT_FORBID_REUSE, 1L);
   
     // Skipping signal handling - results in timeouts not honored during the DNS
     // lookup
     curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
   
     // Timer
     curl_easy_setopt(handle, CURLOPT_TIMEOUT, 3);
   
     // Set User Agent
     curl_easy_setopt(handle, CURLOPT_USERAGENT, "Pulsar-CPP-v2.8.0");
   
     // Redirects
     curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
     curl_easy_setopt(handle, CURLOPT_MAXREDIRS, 20);
   
     // Fail if HTTP return code >=400
     curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L);
   
     struct curl_slist *list = NULL;
     list = curl_slist_append(list, header.c_str());
     curl_easy_setopt(handle, CURLOPT_HTTPHEADER, list);
   
     char errorBuffer[CURL_ERROR_SIZE];
     curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);
   
     curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "GET");
   
     auto res = curl_easy_perform(handle);
     std::cout << "Result: " << res << ", error: " << errorBuffer << std::endl;
   
     long httpResponseCode;
     curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpResponseCode);
     std::cout << "HTTP response code: " << httpResponseCode << std::endl;
   
     curl_easy_cleanup(handle);
     std::cout << "Content:\n" << responseData << std::endl;
   
     return 0;
   }
   ```
   
   You need to modify `completeUrl` and `header` according to your URL.
   
   For example, in my local env, the output could be
   
   ```
   Result: 0, error: 
   HTTP response code: 200
   Content:
   {"brokerUrl":"pulsar://localhost:6650","httpUrl":"http://localhost:8080","nativeUrl":"pulsar://localhost:6650","brokerUrlSsl":""}
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org