解决Server doesn‘t support Accept-Ranges问题

Cannot download differentially, fallback to full download: Error: Server doesn't support Accept-Ranges (response code 200)

解决方案

修改nginx配置文件支持Accept-Ranges(范围请求)

server
{
    ...
    location ^~/ {
        default_type multipart/byteranges;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS,PUT';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        # 支持范围请求
        add_header Accept-Ranges bytes;
        proxy_pass   http://127.0.0.1:5000/;
        proxy_set_header x-forwarded-for  $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

分段请求和差异更新

useMultipleRangeRequest(分段请求)

useMultipleRangeRequest 是 Electron 更新机制中的一个配置选项,用于控制是否启用多段请求(Multiple Range Requests)来下载更新内容。

  • 当启用时,更新程序会通过多个分段请求(Range: bytes=x-y)来下载更新内容,而不是一次性下载整个文件。这种方式可以提高下载效率,尤其是在网络不稳定或文件较大的情况下。

  • 如果服务器不支持多段请求(例如某些服务不支持 Content-Type: multipart/byteranges),则需要禁用此选项。

differentialPackage(差异更新

differentialPackage 是 Electron 构建工具(如 electron-builder)中的一个配置选项,用于启用差异更新(Differential Updates)。

  • 差异更新通过对比新旧版本的文件内容,只下载发生变化的部分,从而减少更新包的大小和下载时间。

  • 这种方式通常通过 blockmap 文件实现,blockmap 文件记录了文件的分块信息(如哈希值和大小),用于确定需要下载的更新内容。

配置示例:

修改main.js文件:
autoUpdater.useMultipleRangeRequest = false;
autoUpdater.differentialPackage = false;
修改package.json:
{
  "publish": [
    {
      "provider": "generic",
      "url": "http://127.0.0.1:5000/updates",
      "useMultipleRangeRequest": false
    }
  ],
  "nsis": {
    "differentialPackage": false
  }
}

/* * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #pragma once #include "esp_err.h" #include "esp_intr_alloc.h" #include "hal/ledc_types.h" #include "driver/gpio.h" #ifdef __cplusplus extern "C" { #endif #if SOC_LEDC_SUPPORT_APB_CLOCK /** * @brief Frequency of one of the LEDC peripheral clock sources, APB_CLK * @note This macro should have no use in your application, we keep it here only for backward compatible */ #define LEDC_APB_CLK_HZ _Pragma ("GCC warning \"'LEDC_APB_CLK_HZ' macro is deprecated\"") (APB_CLK_FREQ) #endif #if SOC_LEDC_SUPPORT_REF_TICK /** * @brief Frequency of one of the LEDC peripheral clock sources, REF_TICK * @note This macro should have no use in your application, we keep it here only for backward compatible */ #define LEDC_REF_CLK_HZ _Pragma ("GCC warning \"'LEDC_REF_CLK_HZ' macro is deprecated\"") (REF_CLK_FREQ) #endif #define LEDC_ERR_DUTY (0xFFFFFFFF) #define LEDC_ERR_VAL (-1) /** * @brief Strategies to be applied to the LEDC channel during system Light-sleep period */ typedef enum { LEDC_SLEEP_MODE_NO_ALIVE_NO_PD = 0, /*!< The default mode: no LEDC output, and no power off the LEDC power domain. */ LEDC_SLEEP_MODE_NO_ALIVE_ALLOW_PD, /*!< The low-power-consumption mode: no LEDC output, and allow to power off the LEDC power domain. This can save power, but at the expense of more RAM being consumed to save register context. This option is only available on targets that support TOP domain to be powered down. */ LEDC_SLEEP_MODE_KEEP_ALIVE, /*!< The high-power-consumption mode: keep LEDC output when the system enters Light-sleep. */ LEDC_SLEEP_MODE_INVALID, /*!< Invalid LEDC sleep mode strategy */ } ledc_sleep_mode_t; /** * @brief Configuration parameters of LEDC channel for ledc_channel_config function */ typedef struct { int gpio_num; /*!< the LEDC output gpio_num, if you want to use gpio16, gpio_num = 16 */ ledc_mode_t speed_mode; /*!< LEDC speed speed_mode, high-speed mode (only exists on esp32) or low-speed mode */ ledc_channel_t channel; /*!< LEDC channel (0 - LEDC_CHANNEL_MAX-1) */ ledc_intr_type_t intr_type; /*!< configure interrupt, Fade interrupt enable or Fade interrupt disable */ ledc_timer_t timer_sel; /*!< Select the timer source of channel (0 - LEDC_TIMER_MAX-1) */ uint32_t duty; /*!< LEDC channel duty, the range of duty setting is [0, (2**duty_resolution)] */ int hpoint; /*!< LEDC channel hpoint value, the range is [0, (2**duty_resolution)-1] */ ledc_sleep_mode_t sleep_mode; /*!< choose the desired behavior for the LEDC channel in Light-sleep */ struct { unsigned int output_invert: 1;/*!< Enable (1) or disable (0) gpio output invert */ } flags; /*!< LEDC flags */ } ledc_channel_config_t; /** * @brief Configuration parameters of LEDC timer for ledc_timer_config function */ typedef struct { ledc_mode_t speed_mode; /*!< LEDC speed speed_mode, high-speed mode (only exists on esp32) or low-speed mode */ ledc_timer_bit_t duty_resolution; /*!< LEDC channel duty resolution */ ledc_timer_t timer_num; /*!< The timer source of channel (0 - LEDC_TIMER_MAX-1) */ uint32_t freq_hz; /*!< LEDC timer frequency (Hz) */ ledc_clk_cfg_t clk_cfg; /*!< Configure LEDC source clock from ledc_clk_cfg_t. Note that LEDC_USE_RC_FAST_CLK and LEDC_USE_XTAL_CLK are non-timer-specific clock sources. You can not have one LEDC timer uses RC_FAST_CLK as the clock source and have another LEDC timer uses XTAL_CLK as its clock source. All chips except esp32 and esp32s2 do not have timer-specific clock sources, which means clock source for all timers must be the same one. */ bool deconfigure; /*!< Set this field to de-configure a LEDC timer which has been configured before Note that it will not check whether the timer wants to be de-configured is binded to any channel. Also, the timer has to be paused first before it can be de-configured. When this field is set, duty_resolution, freq_hz, clk_cfg fields are ignored. */ } ledc_timer_config_t; typedef intr_handle_t ledc_isr_handle_t; /** * @brief LEDC callback event type */ typedef enum { LEDC_FADE_END_EVT /**< LEDC fade end event */ } ledc_cb_event_t; /** * @brief LEDC callback parameter */ typedef struct { ledc_cb_event_t event; /**< Event name */ uint32_t speed_mode; /**< Speed mode of the LEDC channel group */ uint32_t channel; /**< LEDC channel (0 - LEDC_CHANNEL_MAX-1) */ uint32_t duty; /**< LEDC current duty of the channel, the range of duty is [0, (2**duty_resolution)] */ } ledc_cb_param_t; /** * @brief Type of LEDC event callback * @param param LEDC callback parameter * @param user_arg User registered data * @return Whether a high priority task has been waken up by this function */ typedef bool (*ledc_cb_t)(const ledc_cb_param_t *param, void *user_arg); /** * @brief Group of supported LEDC callbacks * @note The callbacks are all running under ISR environment */ typedef struct { ledc_cb_t fade_cb; /**< LEDC fade_end callback function */ } ledc_cbs_t; /** * @brief LEDC channel configuration * Configure LEDC channel with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC duty * * @param ledc_conf Pointer of LEDC channel configure struct * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_channel_config(const ledc_channel_config_t *ledc_conf); /** * @brief Helper function to find the maximum possible duty resolution in bits for ledc_timer_config() * * @param src_clk_freq LEDC timer source clock frequency (Hz) (See doxygen comments of `ledc_clk_cfg_t` or get from `esp_clk_tree_src_get_freq_hz`) * @param timer_freq Desired LEDC timer frequency (Hz) * * @return * - 0 The timer frequency cannot be achieved * - Others The largest duty resolution value to be set */ uint32_t ledc_find_suitable_duty_resolution(uint32_t src_clk_freq, uint32_t timer_freq); /** * @brief LEDC timer configuration * Configure LEDC timer with the given source timer/frequency(Hz)/duty_resolution * * @param timer_conf Pointer of LEDC timer configure struct * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution. * - ESP_ERR_INVALID_STATE Timer cannot be de-configured because timer is not configured or is not paused */ esp_err_t ledc_timer_config(const ledc_timer_config_t *timer_conf); /** * @brief LEDC update channel parameters * * @note Call this function to activate the LEDC updated parameters. * After ledc_set_duty, we need to call this function to update the settings. * And the new LEDC parameters don't take effect until the next PWM cycle. * @note ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to * control one LEDC channel in different tasks at the same time. * A thread-safe version of API is ledc_set_duty_and_update * @note If `CONFIG_LEDC_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker, * makes it possible to execute even when the Cache is disabled. * @note This function is allowed to run within ISR context. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel); /** * @brief Set LEDC output gpio. * * @note This function only routes the LEDC signal to GPIO through matrix, other LEDC resources initialization are not involved. * Please use `ledc_channel_config()` instead to fully configure a LEDC channel. * * @param gpio_num The LEDC output gpio * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t channel); /** * @brief LEDC stop. * Disable LEDC output, and set idle level * * @note If `CONFIG_LEDC_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker, * makes it possible to execute even when the Cache is disabled. * @note This function is allowed to run within ISR context. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param idle_level Set output idle level after LEDC stops. * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level); /** * @brief LEDC set channel frequency (Hz) * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param timer_num LEDC timer index (0-3), select from ledc_timer_t * @param freq_hz Set the LEDC frequency * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution. */ esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz); /** * @brief LEDC get channel frequency (Hz) * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param timer_num LEDC timer index (0-3), select from ledc_timer_t * * @return * - 0 error * - Others Current LEDC frequency */ uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num); /** * @brief LEDC set duty and hpoint value * Only after calling ledc_update_duty will the duty update. * * @note ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to * control one LEDC channel in different tasks at the same time. * A thread-safe version of API is ledc_set_duty_and_update * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)] * @param hpoint Set the LEDC hpoint value, the range is [0, (2**duty_resolution)-1] * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint); /** * @brief LEDC get hpoint value, the counter value when the output is set high level. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * * @return * - LEDC_ERR_VAL if parameter error * - Others Current hpoint value of LEDC channel */ int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel); /** * @brief LEDC set duty * This function do not change the hpoint value of this channel. if needed, please call ledc_set_duty_with_hpoint. * only after calling ledc_update_duty will the duty update. * * @note ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to * control one LEDC channel in different tasks at the same time. * A thread-safe version of API is ledc_set_duty_and_update. * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)] * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty); /** * @brief LEDC get duty * This function returns the duty at the present PWM cycle. * You shouldn't expect the function to return the new duty in the same cycle of calling ledc_update_duty, * because duty update doesn't take effect until the next cycle. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * * @return * - LEDC_ERR_DUTY if parameter error * - Others Current LEDC duty */ uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel); /** * @brief LEDC set gradient * Set LEDC gradient, After the function calls the ledc_update_duty function, the function can take effect. * * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)] * @param fade_direction Set the direction of the gradient * @param step_num Set the number of the gradient * @param duty_cycle_num Set how many LEDC tick each time the gradient lasts * @param duty_scale Set gradient change amplitude * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t fade_direction, uint32_t step_num, uint32_t duty_cycle_num, uint32_t duty_scale); /** * @brief Register LEDC interrupt handler, the handler is an ISR. * The handler will be attached to the same CPU core that this function is running on. * * @param fn Interrupt handler function. * @param arg User-supplied argument passed to the handler function. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred) * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will * be returned here. * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_NOT_FOUND Failed to find available interrupt source */ esp_err_t ledc_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, ledc_isr_handle_t *handle); /** * @brief Configure LEDC timer settings * * This function does not take care of whether the chosen clock source is enabled or not, also does not handle the clock source * to meet channel sleep mode choice. * * If the chosen clock source is a new clock source to the LEDC timer, please use `ledc_timer_config`; * If the clock source is kept to be the same, but frequency needs to be updated, please use `ledc_set_freq`. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param timer_sel Timer index (0-3), there are 4 timers in LEDC module * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source * @param duty_resolution Resolution of duty setting in number of bits. The range is [1, SOC_LEDC_TIMER_BIT_WIDTH] * @param clk_src Select LEDC source clock. * * @return * - (-1) Parameter error * - Other Current LEDC duty */ esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t clock_divider, uint32_t duty_resolution, ledc_clk_src_t clk_src) __attribute__((deprecated("Please use ledc_timer_config() or ledc_set_freq()"))); /** * @brief Reset LEDC timer * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t * * @return * - ESP_ERR_INVALID_ARG Parameter error * - ESP_OK Success */ esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, ledc_timer_t timer_sel); /** * @brief Pause LEDC timer counter * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t * * @return * - ESP_ERR_INVALID_ARG Parameter error * - ESP_OK Success */ esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, ledc_timer_t timer_sel); /** * @brief Resume LEDC timer * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t * * @return * - ESP_ERR_INVALID_ARG Parameter error * - ESP_OK Success */ esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, ledc_timer_t timer_sel); /** * @brief Bind LEDC channel with the selected timer * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t * * @return * - ESP_ERR_INVALID_ARG Parameter error * - ESP_OK Success */ esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_timer_t timer_sel); /** * @brief Set LEDC fade function. * * @note Call ledc_fade_func_install() once before calling this function. * Call ledc_fade_start() after this to start fading. * @note ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to * control one LEDC channel in different tasks at the same time. * A thread-safe version of API is ledc_set_fade_step_and_start * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param target_duty Target duty of fading [0, (2**duty_resolution)] * @param scale Controls the increase or decrease step scale. * @param cycle_num increase or decrease the duty every cycle_num cycles * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num); /** * @brief Set LEDC fade function, with a limited time. * * @note Call ledc_fade_func_install() once before calling this function. * Call ledc_fade_start() after this to start fading. * @note ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to * control one LEDC channel in different tasks at the same time. * A thread-safe version of API is ledc_set_fade_step_and_start * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param target_duty Target duty of fading [0, (2**duty_resolution)] * @param desired_fade_time_ms The intended time of the fading ( ms ). * Note that the actual time it takes to complete the fade could vary by a factor of up to 2x shorter * or longer than the expected time due to internal rounding errors in calculations. * Specifically: * * The total number of cycles (total_cycle_num = desired_fade_time_ms * freq / 1000) * * The difference in duty cycle (duty_delta = |target_duty - current_duty|) * The fade may complete faster than expected if total_cycle_num larger than duty_delta. Conversely, * it may take longer than expected if total_cycle_num is less than duty_delta. * The closer the ratio of total_cycle_num/duty_delta (or its inverse) is to a whole number (the floor value), * the more accurately the actual fade duration will match the intended time. * If an exact fade time is expected, please consider to split the entire fade into several smaller linear fades. * The split should make each fade step has a divisible total_cycle_num/duty_delta (or its inverse) ratio. * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int desired_fade_time_ms); /** * @brief Install LEDC fade function. This function will occupy interrupt of LEDC module. * * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred) * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Intr flag error * - ESP_ERR_NOT_FOUND Failed to find available interrupt source * - ESP_ERR_INVALID_STATE Fade function already installed */ esp_err_t ledc_fade_func_install(int intr_alloc_flags); /** * @brief Uninstall LEDC fade function. */ void ledc_fade_func_uninstall(void); /** * @brief Start LEDC fading. * * @note Call ledc_fade_func_install() once before calling this function. * Call this API right after ledc_set_fade_with_time or ledc_set_fade_with_step before to start fading. * @note Starting fade operation with this API is not thread-safe, use with care. * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel number * @param fade_mode Whether to block until fading done. See ledc_types.h ledc_fade_mode_t for more info. * Note that this function will not return until fading to the target duty if LEDC_FADE_WAIT_DONE mode is selected. * * @return * - ESP_OK Success * - ESP_ERR_INVALID_STATE Channel not initialized or fade function not installed. * - ESP_ERR_INVALID_ARG Parameter error. */ esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode); #if SOC_LEDC_SUPPORT_FADE_STOP /** * @brief Stop LEDC fading. The duty of the channel is guaranteed to be fixed at most one PWM cycle after the function returns. * * @note This API can be called if a new fixed duty or a new fade want to be set while the last fade operation is still running in progress. * @note Call this API will abort the fading operation only if it was started by calling ledc_fade_start with LEDC_FADE_NO_WAIT mode. * @note If a fade was started with LEDC_FADE_WAIT_DONE mode, calling this API afterwards has no use in stopping the fade. Fade will continue until it reaches the target duty. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel number * * @return * - ESP_OK Success * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_ERR_INVALID_ARG Parameter error * - ESP_FAIL Fade function init error */ esp_err_t ledc_fade_stop(ledc_mode_t speed_mode, ledc_channel_t channel); #endif //SOC_LEDC_SUPPORT_FADE_STOP /** * @brief A thread-safe API to set duty for LEDC channel and return when duty updated. * * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)] * @param hpoint Set the LEDC hpoint value, the range is [0, (2**duty_resolution)-1] * * @return * - ESP_OK Success * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_ERR_INVALID_ARG Parameter error * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint); /** * @brief A thread-safe API to set and start LEDC fade function, with a limited time. * * @note Call ledc_fade_func_install() once, before calling this function. * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param target_duty Target duty of fading [0, (2**duty_resolution)] * @param desired_fade_time_ms The intended time of the fading ( ms ). * Note that the actual time it takes to complete the fade could vary by a factor of up to 2x shorter * or longer than the expected time due to internal rounding errors in calculations. * Specifically: * * The total number of cycles (total_cycle_num = desired_fade_time_ms * freq / 1000) * * The difference in duty cycle (duty_delta = |target_duty - current_duty|) * The fade may complete faster than expected if total_cycle_num larger than duty_delta. Conversely, * it may take longer than expected if total_cycle_num is less than duty_delta. * The closer the ratio of total_cycle_num/duty_delta (or its inverse) is to a whole number (the floor value), * the more accurately the actual fade duration will match the intended time. * If an exact fade time is expected, please consider to split the entire fade into several smaller linear fades. * The split should make each fade step has a divisible total_cycle_num/duty_delta (or its inverse) ratio. * @param fade_mode choose blocking or non-blocking mode * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t desired_fade_time_ms, ledc_fade_mode_t fade_mode); /** * @brief A thread-safe API to set and start LEDC fade function. * * @note Call ledc_fade_func_install() once before calling this function. * @note For ESP32, hardware does not support any duty change while a fade operation is running in progress on that channel. * Other duty operations will have to wait until the fade operation has finished. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param target_duty Target duty of fading [0, (2**duty_resolution)] * @param scale Controls the increase or decrease step scale. * @param cycle_num increase or decrease the duty every cycle_num cycles * @param fade_mode choose blocking or non-blocking mode * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num, ledc_fade_mode_t fade_mode); /** * @brief LEDC callback registration function * * @note The callback is called from an ISR, it must never attempt to block, and any FreeRTOS API called must be ISR capable. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param cbs Group of LEDC callback functions * @param user_arg user registered data for the callback function * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_cb_register(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_cbs_t *cbs, void *user_arg); #if SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED /** * @brief Structure for the fade parameters for one hardware fade to be written to gamma wr register * * @verbatim * duty ^ ONE HW LINEAR FADE * | * | * | * | * start_duty + scale * n = end_duty |. . . . . . . . . . . . . . . . . . . . . . . . . .+- * | | * | | * | +--------+ * | | . * | | . * | -------+ . * | . . * | . . * | . . * | . . * ^ --- |. . . . . . . . . .+-------- . * scale| | | . * | | | . * v --- |. . . . .+---------+ . * | | . . * | | . . * start_duty +---------+ . . * | . . . * | . . . * +-----------------------------------------------------------> * PWM cycle * | | | | * | 1 step | 1 step | | * |<------->|<------->| | * | m cycles m cycles | * | | * <---------------------------------------------------> * n total steps * cycles = m * n * @endverbatim * * @note Be aware of the maximum value available on each element */ typedef struct { uint32_t dir : 1; /*!< Duty change direction. Set 1 as increase, 0 as decrease */ uint32_t cycle_num : SOC_LEDC_FADE_PARAMS_BIT_WIDTH; /*!< Number of PWM cycles of each step [0, 2**SOC_LEDC_FADE_PARAMS_BIT_WIDTH-1] */ uint32_t scale : SOC_LEDC_FADE_PARAMS_BIT_WIDTH; /*!< Duty change of each step [0, 2**SOC_LEDC_FADE_PARAMS_BIT_WIDTH-1] */ uint32_t step_num : SOC_LEDC_FADE_PARAMS_BIT_WIDTH; /*!< Total number of steps in one hardware fade [0, 2**SOC_LEDC_FADE_PARAMS_BIT_WIDTH-1] */ } ledc_fade_param_config_t; /** * @brief Set a LEDC multi-fade * * @note Call `ledc_fade_func_install()` once before calling this function. * Call `ledc_fade_start()` after this to start fading. * @note This function is not thread-safe, do not call it to control one LEDC channel in different tasks at the same time. * A thread-safe version of API is ledc_set_multi_fade_and_start * @note This function does not prohibit from duty overflow. User should take care of this by themselves. If duty * overflow happens, the PWM signal will suddenly change from 100% duty cycle to 0%, or the other way around. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param start_duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)] * @param fade_params_list Pointer to the array of fade parameters for a multi-fade * @param list_len Length of the fade_params_list, i.e. number of fade ranges for a multi-fade (1 - SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX) * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_multi_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t start_duty, const ledc_fade_param_config_t *fade_params_list, uint32_t list_len); /** * @brief A thread-safe API to set and start LEDC multi-fade function * * @note Call `ledc_fade_func_install()` once before calling this function. * @note Fade will always begin from the current duty cycle. Make sure it is stable and synchronized to the desired * initial value before calling this function. Otherwise, you may see unexpected duty change. * @note This function does not prohibit from duty overflow. User should take care of this by themselves. If duty * overflow happens, the PWM signal will suddenly change from 100% duty cycle to 0%, or the other way around. * * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param start_duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)] * @param fade_params_list Pointer to the array of fade parameters for a multi-fade * @param list_len Length of the fade_params_list, i.e. number of fade ranges for a multi-fade (1 - SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX) * @param fade_mode Choose blocking or non-blocking mode * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Fade function init error */ esp_err_t ledc_set_multi_fade_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t start_duty, const ledc_fade_param_config_t *fade_params_list, uint32_t list_len, ledc_fade_mode_t fade_mode); /** * @brief Helper function to fill the fade params for a multi-fade. Useful if desires a gamma curve fading. * * @note The fade params are calculated based on the given start_duty and end_duty. If the duty is not at * the start duty (gamma-corrected) when the fade begins, you may see undesired brightness change. * Therefore, please always remember thet when passing the fade_params to either `ledc_set_multi_fade` or * `ledc_set_multi_fade_and start`, the start_duty argument has to be the gamma-corrected start_duty. * * @param[in] speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param[in] channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param[in] start_duty Duty cycle [0, (2**duty_resolution)] where the multi-fade begins with. This value should be a non-gamma-corrected duty cycle. * @param[in] end_duty Duty cycle [0, (2**duty_resolution)] where the multi-fade ends with. This value should be a non-gamma-corrected duty cycle. * @param[in] linear_phase_num Number of linear fades to simulate a gamma curved fade (1 - SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX) * @param[in] max_fade_time_ms The maximum time of the fading ( ms ). * @param[in] gamma_correction_operator User provided gamma correction function. The function argument should be able to * take any value within [0, (2**duty_resolution)]. And returns the gamma-corrected duty cycle. * @param[in] fade_params_list_size The size of the fade_params_list user allocated (1 - SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX) * @param[out] fade_params_list Pointer to the array of ledc_fade_param_config_t structure * @param[out] hw_fade_range_num Number of fade ranges for this multi-fade * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized * - ESP_FAIL Required number of hardware ranges exceeds the size of the ledc_fade_param_config_t array user allocated */ esp_err_t ledc_fill_multi_fade_param_list(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t start_duty, uint32_t end_duty, uint32_t linear_phase_num, uint32_t max_fade_time_ms, uint32_t (* gamma_correction_operator)(uint32_t), uint32_t fade_params_list_size, ledc_fade_param_config_t *fade_params_list, uint32_t *hw_fade_range_num); /** * @brief Get the fade parameters that are stored in gamma ram for a certain fade range * * Gamma ram is where saves the fade parameters for each fade range. The fade parameters are written in during fade * configuration. When fade begins, the duty will change according to the parameters in gamma ram. * * @param[in] speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. * @param[in] channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t * @param[in] range Range index (0 - (SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX-1)), it specifies to which range in gamma ram to read * @param[out] dir Pointer to accept fade direction value * @param[out] cycle Pointer to accept fade cycle value * @param[out] scale Pointer to accept fade scale value * @param[out] step Pointer to accept fade step value * * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE Channel not initialized */ esp_err_t ledc_read_fade_param(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t range, uint32_t *dir, uint32_t *cycle, uint32_t *scale, uint32_t *step); #endif // SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED #ifdef __cplusplus } #endif
最新发布
10-29
安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows PS C:\Users\Administrator> # 以管理员身份打开 PowerShell >> $curlPath = "E:\curl-8.15.0_4-win64-mingw\bin" >> $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") >> >> # 检查是否已添加 >> if (-not $currentPath.Contains($curlPath)) { >> # 添加到系统PATH >> [Environment]::SetEnvironmentVariable( >> "Path", >> "$currentPath;$curlPath", >> "Machine" >> ) >> >> # 刷新当前会话的PATH >> $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") >> >> Write-Host "✅ curl 已添加到系统PATH" -ForegroundColor Green >> } else { >> Write-Host "ℹ️ curl 已在系统PATH中" -ForegroundColor Yellow >> } >> ✅ curl 已添加到系统PATH PS C:\Users\Administrator> # 检查curl版本 >> curl --version >> >> # 预期输出应包含: >> # curl 8.15.0 (x86_64-pc-win32) ... >> # Release-Date: 2025-07-16 >> # Protocols: dict file ftp ftps gopher gophers http https ... >> curl : 未能解析此远程名称: '--version' 所在位置 行:2 字符: 1 + curl --version + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],WebExce ption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand PS C:\Users\Administrator> # 测试HTTP请求 >> curl https://example.com >> >> # 测试HTTPS请求 >> curl https://jsonplaceholder.typicode.com/todos/1 >> >> # 测试文件下载 >> curl -O https://example.com/index.html >> StatusCode : 200 StatusDescription : OK Content : <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" conten... RawContent : HTTP/1.1 200 OK Alt-Svc: h3=":443"; ma=93600,h3-29=":443"; ma=93600 Connection: keep-alive Accept-Ranges: bytes Content-Length: 1256 Cache-Control: max-age=3131 Content-Type: text/html Date: Tu... Forms : {} Headers : {[Alt-Svc, h3=":443"; ma=93600,h3-29=":443"; ma=93600], [Connection, keep-alive], [Accept-Ranges, b ytes], [Content-Length, 1256]...} Images : {} InputFields : {} Links : {@{innerHTML=More information...; innerText=More information...; outerHTML=<A href="https://www.ian a.org/domains/example">More information...</A>; outerText=More information...; tagName=A; href=http s://www.iana.org/domains/example}} ParsedHtml : System.__ComObject RawContentLength : 1256 StatusCode : 200 StatusDescription : OK Content : { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } RawContent : HTTP/1.1 200 OK Connection: keep-alive CF-RAY: 96df6aeb3ca7a3d1-SEA access-control-allow-credentials: true nel: {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fractio... Forms : {} Headers : {[Connection, keep-alive], [CF-RAY, 96df6aeb3ca7a3d1-SEA], [access-control-allow-credentials, true] , [nel, {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fraction":0.01 ,"failure_fraction":0.1}]...} Images : {} InputFields : {} Links : {} ParsedHtml : System.__ComObject RawContentLength : 83 位于命令管道位置 1 的 cmdlet Invoke-WebRequest 请为以下参数提供值: Uri: # 使用完整路径调用真正的 curl curl : 无效的 URI: 未能分析主机名。 所在位置 行:8 字符: 1 + curl -O https://example.com/index.html + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], UriFormatException + FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand PS C:\Users\Administrator> E:\curl-8.15.0_4-win64-mingw\bin\curl.exe --version curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd PS C:\Users\Administrator> PS C:\Users\Administrator> # 测试真正的 curl PS C:\Users\Administrator> E:\curl-8.15.0_4-win64-mingw\bin\curl.exe https://example.com <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> PS C:\Users\Administrator> # 创建永久别名(添加到 PowerShell 配置文件) >> $profileContent = @" >> # 添加 curl 别名 >> function Invoke-Curl { >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" @args >> } >> Set-Alias -Name realcurl -Value Invoke-Curl >> "@ >> >> # 创建配置文件(如果不存在) >> if (-not (Test-Path $PROFILE)) { >> New-Item -Path $PROFILE -ItemType File -Force >> } >> >> # 添加内容到配置文件 >> $profileContent | Add-Content -Path $PROFILE >> >> # 立即加载配置文件 >> . $PROFILE >> >> # 现在可以使用 realcurl 命令 >> realcurl --version >> 目录: C:\Users\Administrator\Documents\WindowsPowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2025/8/12 18:52 0 Microsoft.PowerShell_profile.ps1 curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd PS C:\Users\Administrator> # 临时删除 PowerShell 的 curl 别名 >> Remove-Item Alias:curl -ErrorAction SilentlyContinue >> >> # 现在可以直接使用 curl >> curl --version >> Warning: C:\Users\Administrator\.curlrc:2: warning: '--compressed' the Warning: installed libcurl version doesn't support this curl 8.4.0 (Windows) libcurl/8.4.0 Schannel WinIDN Release-Date: 2023-10-11 Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp Features: AsynchDNS HSTS HTTPS-proxy IDN IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI threadsafe Unicode UnixSockets PS C:\Users\Administrator> # 验证真正的 curl 安装 >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" --version >> >> # 预期输出: >> # curl 8.15.0 (x86_64-pc-win32) ... >> # Release-Date: 2025-07-16 >> # Protocols: dict file ftp ftps gopher gophers http https ... >> curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd PS C:\Users\Administrator> # 使用真正的 curl 下载文件 >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" -O https://example.com/index.html >> >> # 或者使用别名(如果已设置) >> realcurl -O https://example.com/index.html
08-13
<think>根据用户的问题,用户在使用cpp-httplib时遇到了错误提示“32-bit platform unsupported”,需要解决方法或替代方案。首先,我们需要分析这个错误的原因。 查阅cpp-httplib的官方文档和源代码,我们发现该库在较新的版本中移除了对32位平台的支持。具体来说,从某个版本开始,库要求必须是64位系统。这是因为在32位系统上,一些底层数据类型(如`size_t`)可能无法满足处理大文件或大数据的需要,而且维护32位支持会增加代码复杂性。 解决方案可能有以下几种: 1. 降级cpp-httplib版本:使用旧版本的cpp-httplib,因为旧版本可能还支持32位平台。 2. 修改源码:如果用户坚持使用最新版本,可以尝试修改源码,去掉平台检查。 3. 使用其他支持32位的HTTP库作为替代。 下面我们详细说明每种方法: ### 解决方案1:降级cpp-httplib版本 在项目的根目录下执行以下命令(假设使用git管理): ```bash git clone https://github.com/yhirose/cpp-httplib.git cd cpp-httplib # 查找支持32位的版本,例如0.7.15 git checkout v0.7.15 ``` 然后在项目中包含这个旧版本的头文件。 注意:旧版本可能缺少新功能或存在已知问题,需权衡。 ### 解决方案2:修改源码 在cpp-httplib的头文件中,找到平台检查的代码并注释掉。通常,这个检查在文件开头部分。例如,在`httplib.h`中,可能有类似以下代码: ```cpp #if defined(_WIN32) && !defined(_WIN64) #error "This version does not support 32-bit Windows. Please use version 0.7.15 or earlier." #endif ``` 我们可以将这些行注释掉,然后尝试编译。但请注意,这样做可能会导致运行时问题,因为库的后续代码可能依赖64位特性。 ### 解决方案3:使用其他支持32位的HTTP库 如果上述方法不可行,可以考虑其他轻量级的HTTP库,例如: - [Drogon](https://github.com/drogonframework/drogon):一个基于C++17/20的HTTP应用框架,支持32位系统。 - [Pistache](https://github.com/pistacheio/pistache):一个C++ REST框架,支持32位。 - [Crow](https://github.com/ipkn/crow):一个非常简单的C++微框架,支持32位。 - [cpprestsdk (Casablanca)](https://github.com/microsoft/cpprestsdk):微软开源的跨平台HTTP库,支持32位。 以cpprestsdk为例,安装和使用方法如下: 1. 安装依赖(如boost)和cpprestsdk本身。 2. 在项目中包含头文件并链接库。 示例代码: ```cpp #include <cpprest/http_client.h> #include <cpprest/filestream.h> using namespace web; using namespace web::http; using namespace web::http::client; int main() { http_client client(U("http://www.example.com")); client.request(methods::GET).then([](http_response response) { return response.extract_string(); }).then([](std::string body) { std::cout << body << std::endl; }).wait(); return 0; } ``` 注意:替代库的API和功能可能与cpp-httplib有所不同,需要调整代码。 综上所述,用户可以根据自己的情况选择最合适的解决方案。如果项目必须运行在32位平台,且无法修改库,则推荐使用替代库。 为了帮助用户进一步解决问题,以下是相关问题: §§相关问题§§ 1. 如何确定当前系统是32位还是64位? 2. 如何降级cpp-httplib到指定版本? 3. 除了cpp-httplib,还有哪些轻量级的C++ HTTP库推荐? 4. 在Windows 32位系统上编译cpprestsdk需要注意什么?</think>### 解决 cpp-httplib 的 32 位平台不支持错误 #### 错误原因分析 cpp-httplib 从 **v0.10.0 版本开始**移除了对 32 位平台的支持。官方在源码中添加了明确的平台检查: ```cpp #if defined(_WIN32) && !defined(_WIN64) #error "This version does not support 32-bit Windows. Please use version 0.7.15 or earlier." #endif ``` 这是为了简化维护并专注于 64 位系统的优化[^1]。 --- ### 解决方案 #### 方案 1:降级到兼容版本(推荐) 1. **使用旧版本库**: ```bash git clone https://github.com/yhirose/cpp-httplib cd cpp-httplib git checkout v0.7.15 # 最后一个支持32位的版本 ``` 2. **在项目中包含旧版头文件**: ```cpp #include "path/to/cpp-httplib/httplib.h" ``` #### 方案 2:修改源码(风险较高) 1. **注释平台检查** 在 `httplib.h` 中删除或注释掉以下代码: ```cpp // #if defined(_WIN32) && !defined(_WIN64) // #error "32-bit unsupported" // #endif ``` ⚠️ 注意:可能导致运行时错误(如内存溢出),因新版本代码已依赖 64 位特性。 #### 方案 3:使用替代库(长期推荐) 以下轻量级 HTTP 库支持 32 位平台: | 库名称 | 特点 | 安装方式 | |--------|------|----------| | [**Drogon**](https://github.com/drogonframework/drogon) | 异步高性能,支持 HTTP/1.1-2 | `vcpkg install drogon` | | [**Pistache**](https://github.com/pistacheio/pistache) | RESTful API 友好 | `git clone --recursive https://github.com/pistacheio/pistache` | | [**Crow**](https://github.com/ipkn/crow) | 单头文件微框架 | 直接包含 `crow.h` | **示例(使用 Crow 库)**: ```cpp #include "crow.h" int main() { crow::SimpleApp app; CROW_ROUTE(app, "/")([](){ return "Hello 32-bit!"; }); app.port(8080).run(); } ``` --- ### 关键建议 1. **嵌入式场景**:优先使用 **v0.7.15** 旧版 cpp-httplib,资源占用最低[^1]。 2. **新项目开发**:迁移到 **Drogon** 或 **Pistache**,长期维护更有保障。 3. **跨平台需求**:检查 CMake 配置,确保编译选项指定 `-m32`(GCC)或 `/arch:IA32`(MSVC)。 > 📌 引用说明: > [^1]: cpp-httplib 的轻量级特性使其适合嵌入式系统,但新版本已放弃 32 位支持。 > [^2]: 官方 GitHub 明确声明 v0.10.0+ 不再兼容 32 位平台。 --- ### 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值