Browse Source

Specific error messages about extensions are presented without irrelevant HTTP status code

Source commit: 197c7b3064894978f4ac203bc6436df1f6d4b83c
Martin Prikryl 9 years ago
parent
commit
37c5abf4a9
4 changed files with 24 additions and 2 deletions
  1. 8 1
      source/core/Http.cpp
  2. 4 1
      source/core/Http.h
  3. 10 0
      source/forms/Preferences.cpp
  4. 2 0
      source/forms/Preferences.h

+ 8 - 1
source/core/Http.cpp

@@ -13,6 +13,7 @@ THttp::THttp()
 {
   FProxyPort = 0;
   FOnDownload = NULL;
+  FOnError = NULL;
   FResponseLimit = -1;
 
   FRequestHeaders = NULL;
@@ -122,7 +123,13 @@ void THttp::SendRequest(const char * Method, const UnicodeString & Request)
           const ne_status * NeonStatus = ne_get_status(NeonRequest);
           if (NeonStatus->klass != 2)
           {
-            throw Exception(FMTLOAD(HTTP_ERROR2, (NeonStatus->code, StrFromNeon(NeonStatus->reason_phrase), FHostName)));
+            int Status = NeonStatus->code;
+            UnicodeString Message = StrFromNeon(NeonStatus->reason_phrase);
+            if (OnError != NULL)
+            {
+              OnError(this, Status, Message);
+            }
+            throw Exception(FMTLOAD(HTTP_ERROR2, (Status, Message, FHostName)));
           }
 
           void * Cursor = NULL;

+ 4 - 1
source/core/Http.h

@@ -11,6 +11,7 @@ struct ssl_st;
 //---------------------------------------------------------------------------
 class THttp;
 typedef void __fastcall (__closure * THttpDownloadEvent)(THttp * Sender, __int64 Size, bool & Cancel);
+typedef void __fastcall (__closure * THttpErrorEvent)(THttp * Sender, int Status, const UnicodeString & Message);
 //---------------------------------------------------------------------------
 class THttp
 {
@@ -31,7 +32,8 @@ public:
   __property TStrings * ResponseHeaders = { read = FResponseHeaders };
   __property __int64 ResponseLength = { read = GetResponseLength };
   __property __int64 ResponseLimit = { read = FResponseLimit, write = FResponseLimit };
-  __property THttpDownloadEvent OnDownload = { read = FOnDownload, write = FOnDownload};
+  __property THttpDownloadEvent OnDownload = { read = FOnDownload, write = FOnDownload };
+  __property THttpErrorEvent OnError = { read = FOnError, write = FOnError };
 
 private:
   UnicodeString FURL;
@@ -41,6 +43,7 @@ private:
   __int64 FResponseLimit;
   std::unique_ptr<Exception> FException;
   THttpDownloadEvent FOnDownload;
+  THttpErrorEvent FOnError;
   UnicodeString FHostName;
   UnicodeString FCertificateError;
   TStrings * FRequestHeaders;

+ 10 - 0
source/forms/Preferences.cpp

@@ -2428,6 +2428,15 @@ void __fastcall TPreferencesDialog::CustomCommandsViewWindowProc(TMessage & Mess
   }
 }
 //---------------------------------------------------------------------------
+void __fastcall TPreferencesDialog::ExtensionHttpError(THttp * /*Sender*/, int Status, const UnicodeString & Message)
+{
+  if ((Status / 10) == 49)
+  {
+    // HTTP 49x indicate user-friendly error message from winscp.net, throw it without HTTP status code
+    throw Exception(Message);
+  }
+}
+//---------------------------------------------------------------------------
 void __fastcall TPreferencesDialog::AddExtension()
 {
   const UnicodeString HistoryKey(L"ExtensionPath");
@@ -2474,6 +2483,7 @@ void __fastcall TPreferencesDialog::AddExtension()
         std::unique_ptr<TStrings> Headers(new TStringList());
         Headers->Values[L"Accept"] = L"text/winscpextension,text/plain";
         Http->RequestHeaders = Headers.get();
+        Http->OnError = ExtensionHttpError;
         Http->Get();
 
         UnicodeString TrustedStr = Http->ResponseHeaders->Values[L"WinSCP-Extension-Trusted"];

+ 2 - 0
source/forms/Preferences.h

@@ -23,6 +23,7 @@
 //----------------------------------------------------------------------------
 class TCustomCommandList;
 class TEditorList;
+class THttp;
 //----------------------------------------------------------------------------
 class TPreferencesDialog : public TForm
 {
@@ -447,6 +448,7 @@ private:
   int __fastcall GetCommandListIndex(TCustomCommandList * List, int Index);
   int __fastcall GetListCommandIndex(TCustomCommandList * List);
   UnicodeString __fastcall GetSessionKey();
+  void __fastcall ExtensionHttpError(THttp * Sender, int Status, const UnicodeString & Message);
 public:
   virtual __fastcall ~TPreferencesDialog();
   bool __fastcall Execute(TPreferencesDialogData * DialogData);